Buffered input/output stream
BufferedInputStrean 与 BufferedOutputStream类
Cache is a performance optimization of I/O. The cache stream adds a memory buffer area to the I/O stream. With the cache area, the operating efficiency can be greatly improved.
For example, there are two points A and B. There are goods from point A that need to be moved to point B. If manual handling is used, only one goods can be moved at a time, which is very inefficient.
If we change the way and drive a car, if the car is big enough, we can load all the goods and transport them to point B at one time, which greatly improves work efficiency.
The function of the buffer area is equivalent to this car. The use of cached input/output streams is very similar to the use of file input/output streams. Cached input/output streams are nested in file input/output streams.
For example, I want to read the data from the Java API on my desktop. The
code is as follows:
public class Dome {
public static void main(String[] args) {
// 创建文件对象,文件为我位于桌面上的Java,api
File f = new File("C:\\Users\\逢青\\Desktop\\jdk api 1.8.CHM");
FileInputStream in = null;
long start = System.currentTimeMillis();// 程序开始时间
try {
in = new FileInputStream(f);// 创建FileInputStream对象
byte b[] = new byte[1024];// 创建byte数组储存读取的信息
while (in.read(b) != -1) {
// 循环遍历输出,读取信息
}
long end = System.currentTimeMillis();// 程序结束时间
System.out.println("运行经历毫秒值:" + (end - start));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
// 判断in是否为空值,如果不是则关闭流
try {
in.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
This is not adding the cache area, it took 143 milliseconds to
add the cache area
public class Dome {
public static void main(String[] args) {
// 创建文件对象,文件为我位于桌面上的Java,api
File f = new File("C:\\Users\\逢青\\Desktop\\jdk api 1.8.CHM");
FileInputStream in = null;
BufferedInputStream bi = null;
long start = System.currentTimeMillis();// 程序开始时间
try {
in = new FileInputStream(f);// 创建FileInputStream对象
bi = new BufferedInputStream(in);//创建BufferedInputStream对象
byte b[] = new byte[1024];// 创建byte数组储存读取的信息
while (bi.read(b) != -1) {
// 循环遍历输出,读取信息
}
long end = System.currentTimeMillis();// 程序结束时间
System.out.println("运行经历毫秒值:" + (end - start));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bi != null) {
// 判断bi是否为空值,如果不是则关闭流
try {
bi.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
// 判断in是否为空值,如果不是则关闭流
try {
in.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
More than half of the efficiency improvement, it took 35 milliseconds
BufferedReader 与 BufferedWriter类
The biggest feature of buffered character streams BufferedReader and BufferedWriter is that they can be input/output in units of behavior
method | effect |
---|---|
readLine() | Read a line of text |
newLine() | Create new row when writing |
Write an example:
public class Study2 {
public static void main(String[] args) {
File f = new File("word.txt");// 创建文件对象
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(f);// 创建FileWriter对象
bw = new BufferedWriter(fw);// 将文件字符流包装成缓存字符流
String str1 = "抽刀断水水更流";
String str2 = "举杯消愁愁更愁";
bw.write(str1);// 写入第一句话
bw.newLine();// 创建新行,也就是换行
bw.write(str2);// 写入第二句话
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
// 判断bw是否为空值,如果不是则关闭流
try {
bw.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
// 判断fw是否为空值,如果不是则关闭流
try {
fw.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Realize the line break when writing. The
reverse is also possible. Write a few lines in the txt file.
code show as below:
public class Study2 {
public static void main(String[] args) {
File f = new File("word.txt");// 创建文件对象
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(f);// 创建FileReader对象
br = new BufferedReader(fr);// 包装FileReader对象
String tmp = null;
int i = 1;// 计数器
while ((tmp = br.readLine()) != null) {
// 循环读取文件中的信息
System.out.println("第" + i + "行:" + tmp);
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
// 判断br是否为空值,如果不是则关闭流
try {
br.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
// 判断fr是否为空值,如果不是则关闭流
try {
fr.close();// 关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Screenshot of running effect: