Character output stream, the stream buffer

  1. IO stream
    1.1 files operation of the character output stream
    FileWriter file operation of the output character stream
    Constructor constructor
    FileWriter (File File);
    creates a corresponding file in accordance with the File class object file operation of the output character stream
    FileWriter (String pathName);
    creates a corresponding file based on a String file path file operation of the output character stream
    FileWriter (file file, boolean append) ;
    creates a corresponding file based on the file class object file operation output character stream, and the requirements for the additional write
    FileWriter (String pathName, boolean append) ;
    created corresponding to a String type file path file operations character stream output file, and write additional requirements

    If you create a FileWrite object file does not exist here, the legal path, where the operation will create a corresponding file. If the path is invalid, throws an exception FileNotFoundException

Method member method
void write (int ch);
writing data into a file type char
void write (char [] arr) ;
write to a file type array char
void write (char [] arr, int offset, int length );
write to a file char array type, it is required to start reading data from the array at a position offset standard length of
length
void write (string STR);
write a string to a file
void write (string str, int offset , int lenght);
write a string to a file, it is required from the start position of the specified string index offset, length length
if write data during operation, a problem occurs, there will be a IOException

1.2 demo

package com.qfedu.b_io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/*

  • Operation of the character output stream file
    * /
    public class Demo1 {
    public static void main (String [] args) {
    FileWriter FileWriter = null;

     try {
     	fileWriter = new FileWriter(new File("D:/aaa/5.txt"), true);
     	
     	char[] charArray = "充满希望的中国".toCharArray();
     	
     	fileWriter.write(charArray);
     	fileWriter.write("韩国目前疫情情况不容乐观");
     	fileWriter.write("\r\n");
     	fileWriter.write(charArray, 0, 5);
     	fileWriter.write("韩国目前疫情情况不容乐观", 0, 5);
     	
     } catch (IOException e) {
     	e.printStackTrace();
     } finally {
     	if (fileWriter != null) {
     		try {
     			fileWriter.close();
     		} catch (IOException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		}
     	}
     }
    

    }

    private static void writeTest1() {
    FileWriter fileWriter = null;
    try {
    fileWriter = new FileWriter(new File(“D:/aaa/4.txt”), true);

     	fileWriter.write('武');
     	fileWriter.write('汉');
     	fileWriter.write('加');
     	fileWriter.write('油');
     	fileWriter.write(',');
     	fileWriter.write('中');
     	fileWriter.write('国');
     	fileWriter.write('加');
     	fileWriter.write('油');
     	fileWriter.write(',');
     	fileWriter.write('世');
     	fileWriter.write('界');
     	fileWriter.write('加');
     	fileWriter.write('油');
     	
     	
     } catch (IOException e) {
     	// TODO Auto-generated catch block
     	e.printStackTrace();
     } finally {
     	if (fileWriter != null) {
     		try {
     			fileWriter.close();
     		} catch (IOException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		}
     	}
     }
    

    }
    }
    1.3 character stream file copy

    package com.qfedu.b_io;

    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    /*

    • Use file operations copy non-text files character flow problem
    • 【Claim】
    •  禁止使用字符流操作非文本文件,记事本打开乱码文件都不可以
      

    */
    public class Demo2 {
    public static void main(String[] args) {
    FileReader fileReader = null;
    FileWriter fileWriter = null;

     	try {
     		fileReader = new FileReader(new File("D:/aaa/logo桌面.jpg"));
     		fileWriter = new FileWriter(new File("D:/aaa/temp.jpg"));
     		
     		char[] buf = new char[1024 * 4];
     		int length = -1;
     		
     		while ((length = fileReader.read(buf)) != -1) {
     			fileWriter.write(buf, 0, length);
     		}
     	} catch (IOException e) {
     		// TODO Auto-generated catch block
     		e.printStackTrace();
     	} finally {
     		if (fileWriter != null) {
     			try {
     				fileWriter.close();
     			} catch (IOException e) {
     				// TODO Auto-generated catch block
     				e.printStackTrace();
     			}
     		}
     		
     		if (fileReader != null) {
     			try {
     				fileReader.close();
     			} catch (IOException e) {
     				// TODO Auto-generated catch block
     				e.printStackTrace();
     			}
     		}
     	}
     }
    

    }

  1. Buffer flow

2.1 What is the role of buffer flow

使用缓冲数组以后,整体的读取,写入效率提升很大!!!
降低了CPU通过内存访问硬盘的次数。提高效率,降低磁盘损耗。

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

【重点】
	所有的缓冲流都没有任何的读取,写入文件能力,这里都需要对应的输入流和输出流来提供对应的能力。
	在创建缓冲流流对象时,需要传入对应的输入流对象和输出流对象。
	底层就是提供了一个默认大小的缓冲数组,用于提高效率

2.2 byte stream buffer

输入
	BufferedInputStream(InputStream in);	
		这里需要的对象是一个字节输入流基类对象。同时也可也传入InputStream子类对象
输出
	BufferedOutputStream(OutputStream out);
		这里需要的对象是一个字节输出流基类对象。同时也可也传入OutputStream子类对象

以上传入的InputStream和OutputStream都是用于提供对应文件的读写能力。

2.2.1 input stream of bytes buffer efficiency

1. 在BufferedInputStream底层中有一个默认容量为8KB的byte类型缓冲数组。
2. fill方法是一个操作核心
	a. 从硬盘中读取数据,读取的数据容量和缓冲数组容量一致。
	b. 所有的read方法,都是从缓冲数组中读取数据
	c. 每一次读取数据之前,都会检查缓冲区内是否有数据,如果没有,fill方法执行,填充数据。

3. 利用缓冲,fill方法,可以极大的降低CPU通过内存访问硬盘的次数。同时程序操作的数据是在内存中进行交互的。

2.2.2 Efficiency of the bytes output buffer

1. 在BufferedOutputStream类对象,默认有一个8KB的byte类型缓冲数组
2. 数据写入文件时并不是直接保存到文件中,而是保存在内存8KB字节缓冲数组中
3. 如果8KB空间填满,会直接flush缓冲区,数据保存到硬盘中,同时清空整个缓冲区。
4. 在BufferedOutputStream关闭时,首先会调用flush方法,保存数据到文件,清空缓冲区,并且规划缓冲区占用内存,同时关闭缓冲流使用的字节输出流。

2.2.3 unbuffered and buffered stream copy copy time difference efficiency

package com.qfedu.c_buffered;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo3 {
	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		copy();
		
		long end = System.currentTimeMillis();
		// 总耗时
		System.out.println("Time:" + (end - start));
	}
	
	// 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();
				}
			}
		}
 	}
	
    // 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();
				}
			}
		}
	}
}

Character buffer flow 2.3

BufferedReader
	字符缓冲输入流
	BufferedReader(Reader reader);
BufferedWriter
	字符缓冲输出流
	BufferedWriter(Writer writer);

2.3.1 character buffer flow efficiency

1. 字符缓冲输入流,底层有一个8192个元素的缓冲字符数组,而且使用fill方法从硬盘中读取数据填充缓冲数组

2. 字符缓冲输出流,底层有一个8192个元素的缓冲字符数组,使用flush方法将缓冲数组中的内容写入到硬盘当中。

3. 使用缓冲数组之后,程序在运行的大部分时间内都是内存和内存直接的数据交互过程。内存直接的操作效率是比较高的。并且降低了CPU通过内存操作硬盘的次数

4. 关闭字符缓冲流,都会首先释放对应的缓冲数组空间,并且关闭创建对应的字符输入流和字符输出流。

5. 
字符缓冲输入流中
	String readLine(); 读取一行数据
字符缓冲输出流中
	void newLine(); 换行
Released three original articles · won praise 1 · views 22

Guess you like

Origin blog.csdn.net/weixin_44173851/article/details/104546036