Java I / O --- Reader & writer (character stream)

1.Reader & Writer

When we first saw the Reader and Writer classes, we might think that they were two classes to replace InputStream and OutputStream, but they were not.

Although some of the original "stream" class libraries are no longer in use (if you use them, you will receive warnings from the compiler), InputStream and OutputStream can still provide extremely valuable functions in byte oriented I/O, while Reader and Writer provide Unicode and character oriented I/O compatibility.

Sometimes we have to combine classes from the "byte" hierarchy with classes from the "character" hierarchy. To achieve this, the adapter class is used: lnputStreamReader (subclass FileReader) can convert InputStream to Reader (byte to character), and OutputStreamWriter (subclass FileWriter) can convert OutputStream to Writer (character to byte).

       Java I/O -- character and byte conversion stream -- FileReader & filewriter

2. BufferedReader & BufferedWriter

Two classes to realize character stream buffer

 

  1 public class CharStreamBufferDemo {
  2 
  3 	/**
  4 	 * @param args
  5 	 * @throws IOException
  6 	 */
  7 	public static void main(String[] args) throws IOException {
  8 
  9 		/*
 10 		 * Demonstrates the buffer for a character stream.
 11 		 * BufferedReader
 12 		 * BufferedWriter
 13 		 */
 14 //		writeTextByBuffered();
 15 		readTextBuffered();
 16 
 17 //		Read keyboard input specialty. Scanner = stream + regular expression. Methods are reading data according to certain rules.
 18 //		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in))
 19 //		bufr.readLine();
 20 	}
 21 
 22 	public static void readTextBuffered() throws IOException {
 23 
 24 		FileReader fr = new FileReader("tempfile\\bufw.txt");//Convert bytes to characters
 25 
 26 		BufferedReader bufr = new BufferedReader(fr);
 27 
 28 		String line = null;
 29 		while((line=bufr.readLine())!=null){
 30 
 31 			System.out.println(line);
 32 
 33 		}
 34 
 35 		/*String line1 = bufr.readLine();
 36 		System.out.println(line1);
 37 		String line2 = bufr.readLine();
 38 		System.out.println(line2);
 39 		String line3 = bufr.readLine();
 40 		System.out.println(line3);
 41 		String line4 = bufr.readLine();
 42 		System.out.println(line4);
 43 		String line5 = bufr.readLine();
 44 		System.out.println(line5);*/
 45 
 46 		bufr.close();
 47 
 48 	}
 49 
 50 	public static void writeTextByBuffered() throws IOException {
 51 
 52 		//1. Clear purpose.
 53 		FileWriter fw = new FileWriter("tempfile\\bufw.txt");//Convert character to byte
 54 
 55 		//2. Create a buffer object. Specify the stream object to buffer.
 56 		BufferedWriter bufw = new BufferedWriter(fw);
 57 
 58 		for(int x=1; x<=4; x++){
 59 
 60 			bufw.write(x+"abc");
 61 			bufw.newLine();
 62 			bufw.flush();
 63 		}
 64 
 65 //		bufw.write("abc");
 66 //		bufw.newLine();//System.getProperty("line.separator");
 67 //		bufw.write("hello");
 68 		bufw.close();
 69 
 70 	}
 71 
 72 }
 73 

2017-12-31

The content comes from the podcast course of java programming thought

Keywords: Java Programming

Added by nhanlee on Sun, 03 May 2020 16:53:03 +0300