Input stream and output stream of character stream in Java

Character input stream and output stream of java file

In the byte stream before, I introduced you to the input and output, which is relatively simple in terms of the program. The understanding is: the data that has been read from the hard disk to the memory is the input stream, and the data in the memory is stored in the hard disk It is the output stream. The principle of the character stream this time is also the same.

Character stream: The unit of each operation is the input or output of characters. The most commonly used subclasses of FileReader and FileWriter are two byte streams. FileReader is the input stream and FileWriter is the output stream.
1, the character input stream FileReader

Show some below 内联代码片.

	public static void main(String[] args) {
    
    
		FileReader fileReader = null;
		try {
    
    
			fileReader = new FileReader("E:"+File.separator+"ccw.txt");
			//创建输入字符流数组
			char[] charArry =new char[1024*1];
			int len = 0;
			while ((len = fileReader.read(charArry))!= -1) {
    
    
				String string = new String(charArry,0,len);
				System.out.println(string);
			}
		} catch (Exception e) {
    
    
			// TODO: handle exception
		}finally {
    
    
			if (fileReader != null) {
    
    
				try {
    
    
					fileReader.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
2. Character output stream FileWriter

Show some below 内联代码片.

public static void main(String[] args) {
    
    
		FileWriter fileWriter = null;
		try {
    
    
			//true是指在输出的时候不用覆盖文件里已有的数据
			//false表示覆盖文件里已有的数据
			fileWriter = new FileWriter("E:"+File.separator+"ccw.txt",true);
			String str = "IO流分为字节流和字符流,字节流又分为字节输入流字节输出流";
			fileWriter.write(str);
			fileWriter.flush();
		} catch (Exception e) {
    
    
			// TODO: handle exception
		}finally {
    
    
			if (fileWriter != null) {
    
    
				try {
    
    
					fileWriter.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

Guess you like

Origin blog.csdn.net/fdbshsshg/article/details/114001785