IO流
1.IO流概述及其分类
1.概念
- IO流用来处理设备之间的数据传输
- Java对数据的操作是通过流的方式
- Java用于操作流的类都在IO包中
流按流向分为两种:输入流,输出流。
流按操作类型分为两种:
- 字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
- 字符流 : 字符流只能操作纯字符数据,比较方便。
2.IO流常用父类
-
字节流的抽象父类:
InputStream
OutputStream -
字符流的抽象父类:
Reader
Writer
3.IO程序书写
- 使用前,导入IO包中的类
- 使用时,进行IO异常处理
- 使用后,释放资源
2.FileInputStream
2.1.read()
FileInputStream fis = new FileInputStream(“aaa.txt”); //创建一个文件输入流对象,并关联aaa.txt
int b; //定义变量,记录每次读到的字节
while((b = fis.read()) != -1) { //将每次读到的字节赋值给b并判断是否是-1
System.out.println(b); //打印每一个字节
fis.close(); //关闭流释放资源
2.2.read()方法返回值为什么是int
package heima_day20;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo1_IO {
/**
* 程序怕盘中没有创建的文件,所以先将这个异常抛出 throws FileNotFoundException
* @throws IOException
*文件可能存在不可读,所以也将是否可读异常抛出
*
* read()方法读取的是一个字节,为什么返回是int,而不是byte
*
* 00001001 00000100 00100000 11111111 00000000
*
* 10000001 byte类型-1的原码
* 11111110 -1的反码
* 11111111 -1的补码
*
* 00000000 00000000 00000000 11111111 255不会返回-1
*
* 因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111
那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上
24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样可以保证整个数据读完,而结束标记的-1就是int类型
*/
public static void main(String[] args) throws IOException{
//Demo1();
FileInputStream fis = new FileInputStream("xxx.txt");//创建流对象
int b;
while((b = fis.read())!= -1) {
System.out.println(b);
}
}
private static void Demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建流对象
int x = fis.read();//从硬盘上读取一个字节
System.out.println(x);
int y = fis.read();//相当一个指针,每读一次,向后移动一次
System.out.println(y);
fis.close();//关流释放资源
}
}
2.3.write()
package heima_day20;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2_IO {
/**FileOutputStream 字节输出流
* 在创建对象的时候,如果是没有就会帮我们创建
* 如果有这个文件就会将这个文件清空
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//Demo1();
FileOutputStream fos = new FileOutputStream("yyy.txt",true);//如果想续写就在第二个参数传true
fos.write(99);
}
private static void Demo1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("yyy.txt");//创建字节流输出对象,如果没有就自动创建
fos.write(97); //虽然写出的是一个int数,但是到文件上的是一个字节,会自动去除3个8位
fos.write(97);
fos.write(97);
fos.write(100);
fos.close();
}
}
3.拷贝
- FileInputStream读取
- FileOutputStream写出
3.1.第一种和第二种拷贝方式
package heima_day20;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_copy {
/**
* @throws IOException
*
*
*/
public static void main(String[] args) throws IOException {
//copy();
//copy2();
}
private static void copy2() throws FileNotFoundException, IOException {
//第二种拷贝,不推荐使用,会导致内存溢出
FileInputStream fis = new FileInputStream("abc.jpg");
FileOutputStream fos = new FileOutputStream("copy1.txt");
//int len = fis.available();
//System.out.println(len);
byte[] arr = new byte [fis.available()];//创建与文件大小一样的字节数组
fis.read(arr);//将文件上的字节读取到内存中
fos.write(arr);//将字节数组中的字节数据写到文件上
fis.close(); //关流释放资源
fos.close();
}
//第一种拷贝,不推荐使用,效率太低
private static void copy() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联abc.jpg
FileOutputStream fos = new FileOutputStream("zzz.txt");//创建输出流,关联copy.jpg
int b;
while((b = fis.read()) != -1) { //在不断的读取每一字节
fos.write(b); //将每一个字节写出
}
fis.close(); //关流释放资源
fos.close();
}
}
3.2.第三种拷贝方式
package heima_day20;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_copy {
/**
* 第三种拷贝
* 定义小数组
* @throws IOException
*
*/
public static void main(String[] args) throws IOException {
//input1();
//copy3();
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("zzz.txt");
byte[ ] arr = new byte[1024 * 8];
int len;
while((len = fis.read(arr)) != -1) {//如果忘记加arr,返回的就不是读取的字节个数,而是字节的码表值
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void copy3() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("zzz.txt");
byte[ ] arr = new byte[2];
int len;
while((len = fis.read(arr)) != -1) {
fos.write(arr,0,len);
}
fis.close();
fos.close();
}
private static void input1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
byte[] arr = new byte[2];
int a = fis.read(arr);//将文件上的字节读取到字节数组中
System.out.println(a); //读到的有效字节个数
for (byte b : arr) {//第一次获取到文件上的a和b
System.out.println(b);
}
System.out.println("-------");
int c = fis.read(arr);
System.out.println(c);
for (byte b : arr) {
System.out.println(b);
}
fis.close();
}
}
3.3.BufferedInputStream和BufferOutputStream拷贝
-
A:缓冲思想
- 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
- 这是加入了数组这样的缓冲区效果,java本身在设计的时候,
- 也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流
-
B.BufferedInputStream
- BufferedInputStream内置了一个缓冲区(数组)
- 从BufferedInputStream中读取一个字节时
- BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个
- 程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
- 直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
-
C.BufferedOutputStream
- BufferedOutputStream也内置了一个缓冲区(数组)
- 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
- 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
-
D.拷贝的代码
package heima_day20;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_Buffcopy {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("xxx.txt");//创建输入流对象,关联xxx.txt
FileOutputStream fos = new FileOutputStream("copy2.txt");//创建输入流对象,关联copy2.txt
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b;
while((b = bis.read())!= -1) {
bos.write(b);
}
bis.close();
bos.close();
}
}
- E.小数组的读写和带Buffered的读取哪个更快?
- 定义小数组如果是8192个字节大小和Buffered比较的话
- 定义小数组会略胜一筹,因为读和写操作的是同一个数组
- 而Buffered操作的是两个数组
4.flush和close方法的区别
package heima_day20;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6_flushclose {
/**
*
* flush()方法
* 具备刷新功能,刷完之后还能继续写,close刷完就不能继续写
* 用来刷新缓冲区的,刷新后可以再次写出
* close()方法
* 具备刷新功能,在关闭之前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上,再关闭
* 用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,
* 不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("xxx.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));
int b;
while((b = bis.read() ) != -1) {
bos.write(b);
}
bos.flush();
bis.close();
bos.close();
}
}
5.字节流读写中文
字节流读取中文的问题
- 字节流在读中文的时候有可能会读到半个中文,造成乱码
字节流写出中文的问题
- 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
- 写出回车换行 write("\r\n".getBytes());
package heima_day20;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_Chinese {
public static void main(String[] args) throws IOException {
//字节流读取中文
FileInputStream fis = new FileInputStream("yyy.txt");
byte [] arr = new byte [4];
int len;
while((len = fis.read(arr)) != -1) {
System.out.println(new String(arr,0,len));
}
fis.close();
//字节流写中文
FileOutputStream fos = new FileOutputStream("zzz.txt");
fos.write("不好不好".getBytes());
fos.close();
}
}
6.流的标准处理异常代码
package heima_day20;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo8_TryFinally {
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//Demo1();
//JDK1.7
//小括号中的会自动关闭流
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
}
//JDK1.6
private static void Demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("yyy.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}finally {//try finally的嵌套,目的是能关一个尽量关一个
try {
if(fis != null)
fis.close();
}finally {
if(fos != null)
fos.close();
}
}
}
}
- 原理
在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用,流对象的close方法将流关掉