Java foundation - IO stream
In Java, the stream can be understood as a sequence of data: the input stream is used to read data from the origin side; The output stream is used to write data to the destination side.
Console input before JDK5
Multi character reading
Before JDK5, the program obtains the data input of the console end, which is realized through the BufferedReader class, and calls the BufferedReader method of single character reading and writing for many times through a loop Reader () implements the program to obtain multiple characters on the console side.
private static void consoleReadCharacters() throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a character and press'q'Key exit"); char c; do { // read() is used to read a single character input c = (char) br.read(); System.out.println(c); } while (c != 'q'); }
String reading
Before JDK5, the program obtains the data input of the console side, which is realized through the BufferedReader class
private static void consoleReadString() throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter lines of text"); System.out.println("Enter 'end' to quit"); String input; do { input = br.readLine(); System.out.println(input); } while (!input.equals("end")); }
IO stream
For the content covered by the IO stream, the pictures in the rookie tutorial are borrowed here:
In this paper, byte stream and character stream are used to write and read files respectively. It is worth mentioning that: the file input stream is for the current program and is used to read data from the file; Similarly, the file output stream is used to write data to the file.
Byte stream
File output byte stream
The current program outputs the byte stream through the file and writes bytes to the file
/** * Write data to file: output data to file. File is the output party * File output byte stream */ private static void writeStreamToFile() throws IOException { final FileOutputStream outputStream = new FileOutputStream(file); byte[] bytes = {12, 23, 34, 56}; for (int i=0; i<bytes.length; i++) { outputStream.write(bytes[i]); } outputStream.close(); }
File input byte stream
The current program outputs the byte stream through the file and reads bytes from the file
/** * Read data from file: read data from file. File is the input side * Input byte stream */ private static void readStreamFromFile() throws IOException { final FileInputStream inputStream = new FileInputStream(file); final int size = inputStream.available(); for (int i=0; i<size; i++) { System.out.println(inputStream.read()); } inputStream.close(); }
Character stream
Through practice, it can be found that the operation units of byte stream in the process of writing and reading are bytes, which is not so convenient for the operation of Chinese and English characters. At this time, we can consider using character stream operation, which is the package of the above byte stream to facilitate the operation of Chinese and English characters.
File output character stream
/** * Use OutputStreamWriter to write to the file to solve the display garbled code when saving bytes * Output character stream */ private static void writeToFile() throws IOException { // Build the FileOutputStream object. If the file does not exist, it will be created automatically final FileOutputStream outputStream = new FileOutputStream(file); final OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); // Write to cache writer.append("Chinese input"); writer.append("\r\n"); writer.append("English"); // Refresh cache writer.flush(); // Close the output stream and write the data in the cache to the file at the same time writer.close(); outputStream.close(); }
The file output character stream is a package of the file output byte stream, and specifies the encoding format of the file output character stream: UTF-8
File input character stream
/** * Use InputStreamReader to read from the file and solve the garbled code * Input character stream */ private static void readFromFile() throws IOException { // Build FileInputStream object final FileInputStream inputStream = new FileInputStream(file); final InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8"); // Read file contents final StringBuilder builder = new StringBuilder(); while(reader.ready()) { // Append char to StringBuilder object builder.append((char) reader.read()); } // Output content System.out.println(builder.toString()); // Close flow reader.close(); inputStream.close(); }
The file input character stream is a package of the file input byte stream, and the encoding format consistent with the above file output character stream is specified: UTF-8