package IO;
import java.io.*;
/**
* @version V1.0
* @Title:
* @Package
* @Description: (用一句话描述该文件做什么)
* @author: cccwn
* @From: https://blog.csdn.net/cccwn?type=blog
* @date
*/
public class BufferedInputStreamDome {
//
public static void main(String[] args) throws IOException {
//字节缓冲流自带8kb的缓冲区所以性能比较io流要快
//写数据
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myStream\\2323.txt"));
bos.write("abc\r\n".getBytes());
bos.write("qwe\r\n".getBytes());
bos.write("bnm\r\n".getBytes());
bos.close();
//读数据
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myStream\\2323.txt"));
byte[] bytes = new byte[1024];
int by;
while ((by = bis.read(bytes)) != -1){//读取1k字节
//System.out.println(by);读取字节
//转化成字符串
//String(byte[] bytes, int offset, int length, String charsetName)
//构造一个新的 String通过使用指定的字符集解码指定的字节子阵列。
String s = new String(bytes, 0, by,"utf-8");
System.out.println(s);
}bis.close();
}
}
读写问题只要在true就可解决
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myStream\\2323.txt",true));
本文介绍了如何使用Java的BufferedInputStream和BufferedOutputStream进行文件的读写操作,通过缓冲区提升性能。示例代码展示了如何创建缓冲流并进行读写,强调了在追加模式下写入的设置。

2898

被折叠的 条评论
为什么被折叠?



