当前位置:网站首页>BufferedWriter 和 BufferedReader 的使用
BufferedWriter 和 BufferedReader 的使用
2022-06-27 09:33:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
- BufferedWriter
BufferedWriter 起始跟 FileWriter 的使用没什么区别,属于字符输出流。
BufferedWriter 更高效,因为它内置有一个长度为 8192 的字符数组,也就是 8K 的字符数组。这样子,如果我们往文件里面写内容的话,如果内容没有填满这个数组,就会自动等待直到我们填满,然后一起写入硬盘。硬盘的运行速度是很慢的。但是我们也可以利用 close() 方法,虽然它可能没有满,但是还是可以强制让它写入硬盘。
就像黑车司机一样,他拉客,通常总不会拉一个客人就走,一般都是整辆车都坐满了人,才会开车,但有时候实在招不到人了,总不能不走吧。也还是会启动的。
除此之外,BufferedWriter 还提供了自动换行的方法 —— newLine() 方法,它会根据操作系统的不一样,自动增添换行符。在实际开发中,如果要换行,就尽量使用 newLine() 方法。
BufferedWriter 的使用需要借助 FileWriter 来使用:
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("你是我的眼");
bw.newLine();
size++;
if(size==8193)
break;
System.out.println("XXX");
}
System.out.println("YYY");
bw.close();
}
}结果是在一个名为 “file01” 的 txt 文件写入了 8193 行的 “你是我的眼”。
- BufferedReader
BufferedReader 起始跟 FileReader 的使用没什么区别,属于字符输入流。
BufferedReader 更高效,因为它内置有一个长度为 8192 的字符数组,也就是 8K 的字符数组。这样子,如果我们从文件里面读内容的话,如果内容没有填满这个数组,就会自动等待直到我们填满,然后一起从硬盘读到内存。硬盘的运行速度是很慢的。但是我们也可以利用 close() 方法,虽然它可能没有满,但是还是可以强制让它读入内存。
就像供应商运货物到超市,为了省钱,一般不会只装一两个,而是装满了整辆车才上送往超市是吧。但是有时候特殊情况,装不下了,只能再跑一趟了。当我们读的时候,是一个一个的拿出来读的。
除此之外,BufferedReader 还提供了读取一整行的方法 —— readLine() 方法。
BufferedReader 的使用需要借助 FileReader 来使用:
注:一共有三种方法来进行读取
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();
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133643.html原文链接:https://javaforall.cn
边栏推荐
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- 内存泄露的最直接表现
- Object contains copy method?
- Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
- unity--newtonsoft.json解析
- 多网络设备存在时,如何配置其上网优先级?
- CLassLoader
- prometheus告警流程及相关时间参数说明
- Imx8qxp DMA resources and usage (unfinished)
- Five page Jump methods for wechat applet learning
猜你喜欢

ucore lab3

Advanced mathematics Chapter 7 differential equations

Understand neural network structure and optimization methods

Markem Imaje Marken IMAS printer maintenance 9450e printer maintenance

This, constructor, static, and inter call must be understood!

Prometheus alarm process and related time parameter description

prometheus告警流程及相关时间参数说明

有关二叉树的一些练习题

反编译jar包,修改后重新编译为jar包

谷歌浏览器 chropath插件
随机推荐
MySQL proficient-01 addition, deletion and modification
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
IO管脚配置和pinctrl驱动
不会初始化类的几种Case
Object含有Copy方法?
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
Object contains copy method?
CLassLoader
Use CAS to complete concurrent operations with atomic variables
谷歌浏览器 chropath插件
i=i++;
了解神经网络结构和优化方法
Advanced mathematics Chapter 7 differential equations
使线程释放锁资源的操作/方法重载一点注意事项
微信小程序学习之五种页面跳转方法.
Take you to play with the camera module
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
prometheus告警流程及相关时间参数说明
最全H桥电机驱动模块L298N原理及应用
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher