Buffer stream
Buffered streams are also called advanced streams, which are enhancements to the four basic FileXXX streams, so they are also four streams, classified according to data type:
a buffer stream that can read and write efficiently, a conversion stream that can convert encoding, and a persistent storage object Serialized stream
- Byte buffer stream : BufferedInputStream, BufferedOutputStream
- Character buffer stream : BufferedReader, BufferedWriter
Byte buffered input stream [BufferedInputStream]
! ! ! Code on! ! !
public class Demo01BufferedInputStreram {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\cccc.txt"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
bis.close();
}
}
! ! ! result! ! ! The function provided by FileInputStream is the same as reading the file content efficiently
Byte buffered output stream [BufferedOutputStream]
! ! ! Code on! ! !
public class Demo02BufferedOutputStream {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("F:\\dddd.txt", true));
String content = "!!!!Hello World!!!" + "\r\n";
for (int i = 0; i < 10; i++) {
bos.write(content.getBytes());
}
System.out.println("文件输出成功");
bos.close();
}
}
**! ! ! result! ! ! ** The function provided by FileIOnputStream is the same as reading the file content efficiently
Demo CopyFile
! ! ! Enter the code and copy the files! ! ! Don't care what file it is. . . .
public class Demo03CopyFIle {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Media\\MO8EPSG_1.rmvb"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\MO8EPSG_1.rmvb"));
long start = System.currentTimeMillis();
byte[] bytes = new byte[10245];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println("本次复制文件用了:" + (end - start) + "毫秒");
}
}
**! ! ! result! ! ! ** It took only 1 second to copy 1 G file
Character buffered input stream [BufferedReader]
Read text from a character input stream and buffer characters to provide efficient reading of characters, arrays, and lines.
The buffer size can be specified, or the default size can be used. The default value is large enough for most uses.
! ! ! Code on! ! !
public class BufferedReaderAndBufferedWriter {![在这里插入图片描述](https://img-blog.csdnimg.cn/20200217205959345.png)
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("F:\\cccc.txt"));
char[] chars = new char[1024];
int len = 0;
while ((len = br.read(chars)) != -1) {
System.out.println(new String(chars, 0, len));
}
}
}
! ! ! result! ! !
Note:
The input of the specified file will be buffered. There is no buffering, and each call to read () or readLine () may cause bytes to be read from the file, converted to characters, and then returned. This may be very inefficient
. Programs that use DataInputStreams for text input can be replaced by using the appropriate BufferedReader Localize each DataInputStream
Character buffer output stream [BufferedWriter]
Write text to a character output stream, buffering characters to provide efficient writing of single characters, arrays, and strings.
You can specify the buffer size, or you can accept the default size. The default value is large enough for most uses.
! ! ! Code on! ! !
public class Demo02BufferedWriter {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\ffff.txt", true));
String content = "Hello World" + "\r\n";
for (int i = 0; i < 10; i++) {
bw.write(content);
}
bw.close();
}
}
! ! ! result! ! !
Note:
The output of the PrintWriter will be buffered to a file. No buffering, every call to print () method will convert characters to bytes and then write to the file immediately, which may be very inefficient