Buffered stream in Java - learning Summary

1. What is buffered stream, what role?

It should be noted here a little knowledge, at the time of the hard disk read and write operations, a full file read and write speeds much higher than bulk file read and write rates of the same size. This is because the scattered files need to constantly access the hard drive shut down, a great waste of time, the buffer flow is the same principle.

Buffer IO streams based on the stream, using the IO stream. As the name suggests, it is a buffer memory, that is, when the external device to read and write files, create a certain memory, and then read and write data in this part of the memory, and then read and write access to the complete unified device. Stream using a buffer reduces the number of external hard disk access, through a series of tests to prove, the buffer flow may be used several times or even a hundred times to improve the efficiency of reading and writing, in particular for the operation of non-text files.

[Focus]

  • All of them provide buffered stream buffer, no read, write file capabilities, requires a corresponding I, O input and output stream.
  • When creating an object buffer stream flows, the need to pass parameters - corresponding to an input or output stream object stream object.
  • The bottom has a default buffer size of the array, for improving the efficiency

2. Classification

IO buffer stream by stream basis, and IO to flow respectively, the operating unit can be divided into input and output stream, byte, character stream, the buffer flow and therefore have the same classification:

缓冲流
字节缓冲流
字节输入缓冲流
字节输出缓冲流
字符缓冲流
字符输入缓冲流
字符输出缓冲流
	字节输入缓冲流:	BufferedInputStream
	字节输出缓冲流:	BufferedOutputStream
	字符输入缓冲流:	BufferedReader
	字符输出缓冲流:	BufferedWrite

2.1 byte stream buffer

[2.1.1] byte input buffer stream

BufferedInputStream (InputStream inputStream);
parameter input stream of bytes is a base class object. Of course, you can also pass InputStream subclass object

[Efficiency]
  • 1. In the bottom BufferedInputStream default type byte provides a buffer capacity of the array 8KB

  • 2.fill method is a core operation

     a.从硬盘中读取数据,读取的数据容量和缓冲数组容量一致
     b.所有的read方法,都是从缓冲数组中读取数据
     c.每一次读取数据之前,都会检查缓冲区内是否有数据,如果没有,fill方法执行,填充数据,
    
  • 3. The use of buffering, Fill method, can greatly reduce the number of memory access by the CPU in the hard disk, while the data program operation is carried out in memory interact.

[2.1.2] output byte stream buffer

BufferedOutputStream (OutputStream outputStream);
parameter is a base class object output stream of bytes. You can also pass OutputStream subclass object

[Efficiency]
  • 1. same BufferedInputStream, default byte type of buffer array 8kb
  • 2. When writing to the file, the same is to write 8kb buffer memory array, and then saved to the hard disk unified file
  • 3. If the default is not enough capacity 8kb, directly flush the buffer, save data to the hard disk, and emptied the entire buffer, re-use.
  • 4. When BufferedOutputStream closed, first calls the flush method, save the data to a file, empty the buffer, and the buffer memory for programming, while closing the output stream of bytes used by the buffer flow.
Code Demo (copy efficiency)

The method of the buffer array has:


	总耗时1716 ms
	public static void useBuffered() {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(new File("D:/aaa/2.txt")));
			bos = new BufferedOutputStream(new FileOutputStream(new File("D:/aaa/buffered.txt")));
			
			int content = -1;
			
			while ((content = bis.read()) != -1) {
				bos.write(content);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
 	}

Unbuffered array method:


	总耗时531000
	public static void copy() {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try {
			fis = new FileInputStream("D:/aaa/2.txt");
			fos = new FileOutputStream("D:/aaa/copy.txt");
			
			int content = -1;
			
			while ((content = fis.read()) != -1) {
				fos.write(content);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

2.2 character buffer flow

Character buffer stream substantially different from the byte stream buffer

Input character stream buffer: BufferedReader
Output character stream buffer: BufferedWrite

【note】

  • 1. The character buffer the input stream, the bottom by default to provide a 8192 buffer character array elements, and a method using fill from the hard disk to read data to fill the buffer array
  • 2. The character buffer output stream, there is the underlying default provides a 8192 buffer character array elements, the method using flush the contents of the buffer are written to the hard disk array them.
  • 3. After use of a buffer array, most of the time the program is running, all data exchange between memory and memory, reducing the number of memory operations through the CPU drives, high efficiency
  • 4. Close character buffer stream are first released corresponding buffer memory array, and then close the character corresponding to the input stream, the output stream of characters.
  • The character in the input stream buffer String readLine (); row upon
  • 6. The character buffer output stream void newLine (); linefeed
Released seven original articles · won praise 2 · Views 1084

Guess you like

Origin blog.csdn.net/qq_41986648/article/details/104540938