JavaIO总结

这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战

字节流

  1. 概念
    1. 计算机中无论文本,图片,视频都是以二进制(字节)形式存在,针对这些字节的输入出提供的流就成为字节流
    2. 字节流的两个顶级父类
      • InputStream
      • OutPutStream
    3. InputStream API
      方法声明 功能描述
      int read() 从输入流中读取一个8位的字节,转换成0-255之间的一个整数
      int read(byte[] b)
      int read(byte[] b,int off,int len)
      void close() 关闭流
    4. OutPutStream API
      方法声明 功能描述
      void write(int b) 向输出流写入一个字节
      void write(byte[] b)
      void write(byte[] b,int off,int len)
      void flush() 刷新并强制写出所有缓冲的字节
      void close()
  2. 字节流的体系结构


3. 字节流读取文件

  1. 两个类
    • FileInputStream
    • FileOutPutStream
  2. 读文件
public class Demo01 {
 public static void main(String[] args) throws IOException {
 	FileInputStream in = new FileInputStream("G:\\testFile\\java\\best2.txt");
 	int b = 0;
 	while(true) {
 	//变量b记住每次读取的一个字节
 		b = in.read();
 		if(b == -1) {
 			break;
 		}
 		System.out.print((char)b);
 	}
 	in.close();
 }
}
复制代码
  1. 写文件
public class Demo01 {
 public static void main(String[] args) throws IOException {
 	FileOutputStream out = new
 	//如果目录存在best2文件则会覆盖
 	FileOutputStream("G:\\testFile\\java\\best2.txt");
 	//如果目录存在best2文件则会追加到源文件后面
 	//FileOutputStream("G:\\testFile\\java\\best2.txt",true);
 	String str = "这是一个流!!!";
 	byte[] b = str.getBytes();
 	for(int i = 0; i<b.length; i++) {
 	//注意这里是b[i],而不是b
 		out.write(b[i]);
 	}
 	out.close();
 }
}
复制代码
  1. 文件的拷贝
    1. 经常会通过输入流输出流合作进行文件拷贝
    public class Demo2 {
     public static void main(String[] args) throws Exception {
     	InputStream in = new FileInputStream("G:\\testFile\\java\\source\\需求说明书v1.0.docx");
     	OutputStream out = new FileOutputStream("G:\\testFile\\java\\target\\需求说明书v1.0.docx"); 
     	double beginTime= System.currentTimeMillis();
     	int len;
     	//用len保存一个字节,直到最后一个字节
     	while((len = in.read()) != -1) {
     		out.write(len);
     	}
     	double endTime = System.currentTimeMillis();
     	System.out.println("耗时" + (endTime - beginTime)/1000);
     	in.close();
     	out.close();
     		
     }
    }
    复制代码
  2. 字节流的缓冲区
    1. 作用:可以一次性读取多个字节,存到数组中,然后一下子写入文件里,大大提高效率
    public class Demo2 {
     public static void main(String[] args) throws Exception {
     	InputStream in = new FileInputStream("G:\\testFile\\java\\source\\需求说明书v1.0.docx");
     	OutputStream out = new FileOutputStream("G:\\testFile\\java\\target\\需求说明书v1.0.docx"); 
     	//定义一个数组缓冲区
     	byte[] b = new byte[1024];
     	double beginTime= System.currentTimeMillis();
     	//用len记住数组的最后一个索引,-1时结束
     	int len;
     	//每循环一次就把数组填满
     	while((len = in.read(b)) != -1) {
     		//从缓冲区里读,从o开始到len结束
     		out.write(b,0,len);
     	}
     	double endTime = System.currentTimeMillis();
     	System.out.println("耗时" + (endTime - beginTime)/1000);
     	in.close();
     	out.close();
     		
        }
    }
    复制代码
  3. 字节缓冲流
    1. 主要类
      • BufferedInputStream
      • BufferedOutputStream
      • 这两个流内部都定义了一个大小为8192的字节数组(就不用手动定义缓冲区了)
    2. 实现
    public class Demo3 {
     public static void main(String[] args) throws Exception {
     //创建了一个带有缓冲区的输入流,输出流
     //这里把文件输入流传进缓冲输入流中,使用了装饰器模式
     	BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:\\\\testFile\\\\java\\\\source\\\\需求说明书v1.0.docx"));
     	BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:\\testFile\\java\\target\\需求说明书v1.0.docx"));
     	int len;
     	while((len = bis.read()) == -1) {
     		bos.write(len);
     	}
     	bis.close();
     	bos.close();
        }
    }
    复制代码

字符流

  • 字符流只能对字符(即文本内容)进行操作
  1. 概念
    1. 两个顶级父类
      • Reader
      • Writer
    2. 体系结构

  2. 字符流操作文件
    • FileWriter
    • FileReader
    1. 读文件,类似字节流
    public class Demo4 {
     public static void main(String[] args) throws Exception {
     	FileReader reader = new FileReader("G:\\testFile\\java\\best2.txt");
     	int len;
     	while((len = reader.read()) != -1) {
     		System.out.print((char)len);
     	}
     	reader.close();
    	}
    }
    复制代码
    1. 写文件
    public class Demo4 {
     public static void main(String[] args) throws Exception {
     	FileWriter writer = new FileWriter("G:\\testFile\\java\\best3.txt");
     	//同上,文件就不会覆盖了,追加在后面
     	//FileWriter writer = new FileWriter("G:\\testFile\\java\\best3.txt",true);
     	String str = "你好啊";
     	writer.write(str);
     	writer.close();
        }
    }
    复制代码
    1. 字符缓冲流
      • 如果最后没有执行close,那么缓冲区里的东西很可能就会写到文件中去
    2. LineNumberReader
      • 时BufferedReader的直接子类
      • 是一个可以跟踪行号的输入流
    public class Demo5 {
       public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("G:\\\\testFile\\\\java\\\\best3.txt");
        FileWriter fw = new FileWriter("G:\\\\testFile\\\\java\\\\best4.txt");
        LineNumberReader lr = new LineNumberReader(fr);
        lr.setLineNumber(0);
        String line = null;
        while((line = lr.readLine()) != null) {
     	  fw.write(lr.getLineNumber() + " " + line);
     	  fw.write("\n");
        }
        lr.close();
        fw.close();
     
        }
    }
    复制代码