当前位置:网站首页>Use of bufferedwriter and BufferedReader
Use of bufferedwriter and BufferedReader
2022-06-27 09:40:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
- BufferedWriter
BufferedWriter Starting heel FileWriter There is no difference in the use of , It belongs to character output stream .
BufferedWriter More efficient , Because it has a built-in length of 8192 Array of characters , That is to say 8K Array of characters . It looks like , If we write something in the document , If the contents do not fill the array , Will automatically wait until we fill up , Then write to the hard disk together . The running speed of hard disk is very slow . But we can also use close() Method , Although it may not be full , But you can still force it to write to the hard disk .
Like a black car driver , He solicits customers , Usually you don't pull a guest away , Usually the whole car is full of people , To drive , But sometimes I can't find anyone , I have to go . It will still start .
besides ,BufferedWriter It also provides a way to wrap lines automatically —— newLine() Method , It will vary according to the operating system , Automatically add line breaks . In actual development , If you want a new line , Try to use newLine() Method .
BufferedWriter The use of requires the help of FileWriter To use :
public class TestBufferedWriter {
public static void main(String[] args) throws IOException{
FileWriter fw = new FileWriter("file01.txt");
BufferedWriter bw = new BufferedWriter(fw);
int size = 0;
while(true) {
bw.write(" You are my eye ");
bw.newLine();
size++;
if(size==8193)
break;
System.out.println("XXX");
}
System.out.println("YYY");
bw.close();
}
}The result is in a named “file01” Of txt File written 8193 Yes “ You are my eye ”.
- BufferedReader
BufferedReader Starting heel FileReader There is no difference in the use of , Belongs to the character input stream .
BufferedReader More efficient , Because it has a built-in length of 8192 Array of characters , That is to say 8K Array of characters . It looks like , If we read from the file , If the contents do not fill the array , Will automatically wait until we fill up , Then read from the hard disk to the memory together . The running speed of hard disk is very slow . But we can also use close() Method , Although it may not be full , But you can force it to read into memory .
It's like a supplier shipping goods to a supermarket , To save money , Generally, there are not only oneortwo , Instead, the whole car is filled before it is sent to the supermarket . But sometimes special circumstances , I can't fit it , Only one more trip . When we read , It was read one by one .
besides ,BufferedReader It also provides a way to read an entire row —— readLine() Method .
BufferedReader The use of requires the help of FileReader To use :
notes : There are three ways to read
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("file01.txt");
BufferedReader br = new BufferedReader(fr);
/*
int ch;
while((ch=br.read())!=-1) {
System.out.print((char)ch);
if((char)ch == '\n')
System.out.println();
}
*/
String str;
while((str=br.readLine())!=null)
System.out.println(str);
/*char[] a = new char[8193];
int len;
while((len=br.read(a))!=-1) {
String str = new String(a,0,len);
System.out.println(str);
}
System.out.print("********");
*/
br.close();
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133643.html Link to the original text :https://javaforall.cn
边栏推荐
- Use aspese Cells convert Excel to PDF
- 【系统设计】邻近服务
- Freemarker
- main()的参数argc与argv
- Unity - - newtonsoft. Analyse json
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- 小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行center中心化(每个数据列减去平均值)、设置method参数为center
- Prometheus alarm process and related time parameter description
- CLassLoader
猜你喜欢
随机推荐
Conception de plusieurs classes
unity--newtonsoft. JSON parsing
Freemarker
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
【系统设计】邻近服务
BufferedWriter 和 BufferedReader 的使用
更改pip镜像源
Rockermq message sending mode
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
【OpenCV 例程200篇】212. 绘制倾斜的矩形
openpyxl表格读取实例
When multiple network devices exist, how to configure their Internet access priority?
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy
Apache POI的读写
MySQL proficient-01 addition, deletion and modification
torchvision.models._utils.IntermediateLayerGetter使用教程
借助原子变量,使用CAS完成并发操作
Use CAS to complete concurrent operations with atomic variables
R语言plotly可视化:可视化多个数据集归一化直方图(historgram)并在直方图中添加密度曲线kde、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图









