Streaming IO is a traditional IO, which reads information from one place and outputs it to another place by constructing input and output streams. Common are reading files and writing files.
Basic API
The lost IO can be basically divided into two sects, an old IO represented by InputStream and OutputStream and a new IO represented by Reader and Writer.
Only the most commonly used APIs are shown here, and other APIs can be consulted jdk API
input
Basic input
InputStream | Reader |
---|---|
InputStream oriented byte stream IO does not support Unicode characters well | Reader oriented character stream IO can well support Unicode characters |
FileInputStream file read stream | FileReader file read |
ByteArrayInputStream | CharArrayReader |
Decorator input
The flow object in the basic input can be used as the constructor parameter of the decorator object
InputStream | Reader | function |
---|---|---|
DataInputStream | Contains all interfaces for reading basic data types | |
BufferedInputStream | BufferedReader | In essence, it does not provide an interface, but just adds buffering function to the process. Collocation with interface objects |
output
Basic output
OutputStream | Writer |
---|---|
FileOutputStream | FileWriter |
ByteArrayOutputStream | CharArrayWriter |
Decorator output
OutputStream | Writer | function |
---|---|---|
DataOutputStream | Contains all interfaces for writing basic data types | |
PrintStream | PrintWriter | Used to generate formatted output |
BufferedOutputStream | BufferedWriter | In essence, it does not provide an interface, but adds buffering to the process. Collocation with interface objects |
Common usage
read file
Read using FileInputStream
The following example puts the input stream into the try with resource block to realize the automatic shutdown of resources. This form will be adopted in the following examples in this paper.
It can be seen here that it is read byte by byte, so it can be displayed normally only when it is converted to char, otherwise all displayed are bytes. Since InputStream is a byte stream, the Chinese read here shows garbled code.
public class Read { /** * Read directly using FileInputStream * Because InputStream does not support Unicode encoding, the Chinese display will be garbled */ public static void fileInputStream() { try ( FileInputStream input = new FileInputStream("Read.java") ) { int n = 0; while (n != -1) { n = input.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileInputStream(); } }
Output:
package cyrus.file_io.iostream; import java.io.FileInputStream; public class Read { /** * ä½¿ç¨ FileInputStream ç´æ¥è¯»å * ç±äº InputStream ä¸æ¯æ Unicode ç¼ç ï¼æ以ä¸ææ¾ç¤ºä¼ä¹±ç */ public static void fileInputStream() { try ( FileInputStream input = new FileInputStream("Read.java") ) { int n = 0; while (n != -1) { n = input.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileInputStream(); } }
Read with BufferedInputStream decorator
The following example uses FileInputStream to construct a BufferedInputStream decorator. The main function of the adapter is to fill the read content into the buffer. The other usage is the same as FileInputStream. InputStream is a byte stream, so the Chinese read here shows garbled code.
public class Read { /** * Use FileInputStream to construct a BufferedInputStream decorator and read, which will use the buffer * Because InputStream does not support Unicode encoding, Chinese will be garbled */ public static void fileInputStreamWithBuffer() { try ( BufferedInputStream input = new BufferedInputStream(new FileInputStream("Read.java")) ) { int n = 0; while (n != -1) { n = input.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileInputStreamWithBuffer(); } }
Output:
package cyrus.file_io.iostream; import java.io.BufferedInputStream; import java.io.FileInputStream; public class Read { /** * ä½¿ç¨ FileInputStream æé ä¸ä¸ª BufferedInputStream è£ é¥°å¨ï¼è¯»åï¼è¯¥è¯»åä¼ä½¿ç¨ç¼å²åº * ç±äº InputStream ä¸æ¯æ Unicode ç¼ç ï¼æ以ä¸æä¼ä¹±ç */ public static void fileInputStreamWithBuffer() { try ( BufferedInputStream input = new BufferedInputStream(new FileInputStream("Read.java")) ) { int n = 0; while (n != -1) { n = input.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileInputStreamWithBuffer(); } }
Read using FileReader
Use FileReader to read directly. Here, the Reader supports Unicode coding, so Chinese will not be garbled and will be displayed normally
public class Read { /** * Read directly using FileReader * Since the Reader supports Unicode encoding, Chinese will not be garbled and will be displayed normally */ public static void fileReader() { try ( FileReader reader = new FileReader("Read.java") ) { int n = 0; while (n != -1) { n = reader.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileReader(); } }
Output:
package cyrus.file_io.iostream; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileReader; public class Read { /** * Read directly using FileReader * Since the Reader supports Unicode encoding, Chinese will not be garbled and will be displayed normally */ public static void fileReader() { try ( FileReader reader = new FileReader("Read.java") ) { int n = 0; while (n != -1) { n = reader.read(); char c = (char) n; System.out.print(c); } } catch (Exception e) { } } public static void main(String[] args) { Read.fileReader(); } }
Read with BufferedReader decorator
Here, FileReader is used to construct a BufferedReader decorator. The main function of BufferedReader is to fill the read content into the buffer, and the lines() method of BufferedReader will return a stream stream, which is more convenient to operate.
public class Read { /** * Use FileReader to construct a BufferedReader decorator to read, which will use the buffer * Since the Reader supports Unicode encoding, Chinese will not be garbled and will be displayed normally */ public static void fileReaderWithBuffer() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { reader.lines().forEach(System.out::println); } catch (Exception e) { } } public static void main(String[] args) { Read.fileReaderWithBuffer(); } }
Output:
package cyrus.file_io.iostream; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; public class Read { /** * Use FileReader to construct a BufferedReader decorator to read, which will use the buffer * Since the Reader supports Unicode encoding, Chinese will not be garbled and will be displayed normally */ public static void fileReaderWithBuffer() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { reader.lines().forEach(System.out::println); } catch (Exception e) { } } public static void main(String[] args) { Read.fileReaderWithBuffer(); } }
Use the DataInputStream adapter to read strings
Here, the buildString() method reads the current file and assembles it into a string output. ByteArrayInputStream reads the byte array of the string, and then converts it into a DataInputStream adapter to read the string content.
DataInputStream is mainly used to read basic data types
public class Read { /** * Use ByteArrayInputStream to construct DataInputStream decorator and output string information */ public static void dataInputStream() { try ( DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buildString().getBytes())) ) { while (inputStream.available() != 0) { System.out.print((char) inputStream.readByte()); } } catch (Exception e) { } } /** * Build a string from the current java file * * @return */ public static String buildString() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { } return "ok"; } public static void main(String[] args) { Read.dataInputStream(); } }
write file
Write using FileOutputStream
Here, directly use FileOutputStream to read the string constructed by the buildString() method and write it to the Read.txt file
public class Read { /** * Write string directly using FileOutputStream */ public static void fileOutputStream() { try ( FileOutputStream output = new FileOutputStream("Read.txt") ) { output.write(buildString().getBytes()); } catch (Exception e) { } } /** * Build a string from the current java file * * @return */ public static String buildString() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { } return "ok"; } public static void main(String[] args) { Read.fileOutputStream(); } }
Output: part of the instance screenshot
Write using BufferedOutputStream adapter
Here, a BufferedOutputStream adapter is constructed using FileOutputStream. BufferedOutputStream will use the buffer to speed up reading and writing.
public class Read { /** * Use FileOutputStream to construct a BufferedOutputStream decorator. Read and write will use the buffer */ public static void fileOutputStreamWithBuffer() { try ( BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("Read.txt")); ) { output.write(buildString().getBytes()); } catch (Exception e) { } } /** * Build a string from the current java file * * @return */ public static String buildString() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { } return "ok"; } public static void main(String[] args) { Read.fileOutputStreamWithBuffer(); } }
The output will not be shown later. All the output is output to the Read.txt folder of the current working directory, and the file content is the content of the current. java file
Write using FileWriter
public class Read { /** * Write directly using FileWriter */ public static void fileWriter() { try ( FileWriter writer = new FileWriter("Read.txt"); ) { writer.write(buildString()); } catch (Exception e) { } } /** * Build a string from the current java file * * @return */ public static String buildString() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { } return "ok"; } public static void main(String[] args) { Read.fileWriter(); } }
Output using PrintWriter
Two layers of adapters are used here. The first layer is to use FileWriter to construct a BufferedWriter, which will write using the buffer, and then use BufferedWriter to construct a PrintWriter adapter, which will output directly to the specified file using the println() method of the adapter.
public class Read { /** * Use FileWriter to construct a BufferedWriter decorator, which will use buffer, then use BufferedWriter to construct a PrintWriter decorator, and use println to output to file */ public static void fileWriterWithBuffer() { try ( PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("Read.txt"))) ) { writer.println(buildString()); } catch (Exception e) { } } /** * Build a string from the current java file * * @return */ public static String buildString() { try (BufferedReader reader = new BufferedReader(new FileReader("Read.java"))) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { } return "ok"; } public static void main(String[] args) { Read.fileWriterWithBuffer(); } }
summary
Streaming IO is actually quite understandable. It is divided into two sets. InputStream and OutputStream are one set, and Reader and Writer are one set. Each of them has a basic input-output class and a decorator input-output class constructed using the basic input-output class. Decorators can also construct decorators and unlimited sets of dolls.
For example, new PrintWriter(new BufferedWriter(new FileWriter("Read.txt")), here the BufferedWriter decorator is assembled into a PrintWriter decorator, so that the PrintWriter has the characteristics of BufferedWriter (using buffer) and its own characteristics.
This article provides some personal opinions in my learning process. Loopholes are essential. I hope you can give me more advice and help repair loopholes!!!
Reference: java programming ideas
Welcome to my document for a clearer format:
https://www.yuque.com/docs/share/26bf2f91-b3d3-4b28-a43e-302a1e650d57?# Streaming IO