java 缓冲流
java 缓冲流

BufferedOutputStream:字节缓冲输入流

  • 该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中 * 而不必针对每次字节写入调用底层系统。
  • 父类:FilterOutputStream * 从以下版本开始: JDK 1.0 * *
    操作系统不同,换行符不一样 * (2)Windows系统里面,每行结尾是 回车+换行(CR+LF),即“\r\n” *
    (3)Unix系统里,每行结尾只有 换行LF,即“\n”; * (4)Mac系统里,每行结尾是 回车CR 即’\r’。

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo {

	public static void main(String[] args) throws IOException {
		
		//1:创建字节缓冲输出流  包装流 (底层包装的是字节输出流   称之为节点流)
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c.txt"));
		//2:往文件写数据
		//2.1 一次写一个字节
	
		//2.2 一次写一个字节数组
		bos.write(new byte[]{97,98,99,100,101});
		
		bos.write("\r\n".getBytes());
		
		bos.write(new byte[]{97,98,99,100,101});
		
		
		//3:关闭流  只需关闭高层流即可
		bos.close();
		bos.write(65);
		
		
		
	}

}

BufferedinputStream:字节缓冲输入流

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo {
	
	public static void main(String[] args) throws IOException {
		//1:创建字节缓冲输入流  (包装流,底层包装的是字节缓冲流)
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c.txt"));
		//2:读取数据
		 //2.1 一次读取一个字节
		/* int read = bis.read();
		 System.out.println((char)read);*/
		/*int num = 0;
		while((num = bis.read())!=-1){
			System.out.print((char)num);
		}*/
		 //2.2 一次读取一个字节数组
		int num = 0;
		 byte[] by = new byte[1024];
		 while((num = bis.read(by))!=-1){
			 System.out.print(new String(by,0,num));
		 }
		//3:关闭流
		bis.close();
		
		
		
	}

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2021-09-28
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-23
  • 2021-10-25
  • 2021-07-18
  • 2021-07-18
  • 2022-12-23
相关资源
相似解决方案