Let's first look at the main categories of IO streams Very many (numb. jpg)
catalogue
A node flow and a processing flow
three Standard input / output stream
A node flow and a processing flow
1.1 concept
Node flow: node flow is a flow that reads and writes to a single data source (array file, pipeline string) Such as FileReader FileWriter dedicated to reading and writing files
Processing flow: processing flow is also called wrapper flow Connect to an existing stream (node stream or wrapper stream) Provide more powerful read and write functions for programs And more flexible The data source can be an array or a file
ps: node flow is equivalent to a craftsman who can only make chairs and can only operate on wood
The process flow is equivalent to a multifunctional craftsman You can operate on wood You can also use stones Operate with sand
Maybe the description is not accurate But that's roughly what it means The processing flow can process more than the node flow
The BufferedReader class has the attribute Reader, which can encapsulate a node flow. The node flow can be any Reader subclass
The BufferedWriter class has the attribute Writer, which can encapsulate a node flow. The node flow can be any subclass of Writer
Differences and relations between node flow and processing flow
1. Node flow is the bottom flow / low-level flow directly connected to the data source (file pipeline)
2. Processing flow wrapping node flow can not only eliminate the implementation differences of different node flows, but also provide a more convenient method to complete input and output
3. The processing flow wraps the node flow using the decorator design pattern and will not be directly connected to the data source
Note: the processing flow is a node flow The node stream is directly connected to the data source Therefore, the processing flow is indirectly connected to the data source
The function of processing flow is mainly reflected in the following two aspects
1. Performance improvement: mainly increase the buffer to improve the efficiency of input and output
Note: Buffered provides an eight byte buffer When the program interacts with the disk The efficiency is very low and the speed is very slow. Ordinary streams read and write one byte at a time The buffer stream sets a buffer in memory After storing enough data in the buffer Then exchange with memory or disk Reduces the number of exchanges required Improved processing efficiency
2. Convenient operation: the processing flow may provide a series of convenient methods to input and output a large number of data at one time, which is more flexible and convenient
1.2 character stream
1.2.1 BufferedReader
public static void main(String[] args) throws IOException { String filepath="d:\\1.txt"; FileReader reader = new FileReader(filepath); BufferedReader bufferedReader = new BufferedReader(reader); //One character one character read in int readline; while((readline=bufferedReader.read())!=-1){ System.out.print((char)(readline)); } //Read in line by line String read; while ((read=bufferedReader.readLine())!=null){ System.out.println(read); } //Just turn off the outer flow reader.close(); }
1.2.2 BufferedWriter
public static void main(String[] args) throws IOException { String filepath = "d:\\1.txt"; FileWriter fileWriter = new FileWriter(filepath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write("Sophomore internship Chong Chong"); bufferedWriter.write("15"); bufferedWriter.write('F'); bufferedWriter.close(); }
1.2.3 Buffered copy
public static void main(String[] args) throws IOException { //Copy while reading and writing String filepath1 = "d:\\hello.txt";//Read in stream String filepath2 = "d:\\1.txt";//Output stream FileReader fileReader = new FileReader(filepath1); FileWriter fileWriter = new FileWriter(filepath2); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); BufferedReader bufferedReader = new BufferedReader(fileReader); String readline; while ((readline=bufferedReader.readLine())!=null){ bufferedWriter.write(readline); } bufferedReader.close(); bufferedWriter.close(); }
1.3 byte stream
1.3.1BufferedInputStream
public static void main(String[] args) throws IOException { String filepath = "d:\\hello.txt"; FileInputStream fileInputStream = new FileInputStream(filepath); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //Read by byte int read; while ((read=bufferedInputStream.read())!=-1){ System.out.print((char) read); } //Byte array read byte[] buf = new byte[1024]; int readbuf; while((readbuf=bufferedInputStream.read(buf))!=-1){ System.out.println(new String(buf,0,readbuf)); } bufferedInputStream.close(); }
1.3.2 BufferedOutputStream
public static void main(String[] args) throws IOException { String filepath = "d:\\hello.txt"; FileOutputStream fileOutputStream = new FileOutputStream(filepath); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); bufferedOutputStream.write("I LOVE YOU THREE THOUSANDS TIMES".getBytes());//Convert to byte array bufferedOutputStream.close(); }
1.3.3 Byte stream copy
public static void main(String[] args) throws IOException { //Two addresses String filepath = "d:\\hello.txt"; String filepath1 = "d:\\hsp.txt"; FileInputStream fileInputStream = new FileInputStream(filepath); FileOutputStream fileOutputStream = new FileOutputStream(filepath1); byte buf[]=new byte[1024]; int readlen; while((readlen=fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,readlen); } fileInputStream.close(); fileOutputStream.close(); }
two Object processing flow
demand
Save a dog class object. Dog dog = new Dog("little flower farmer", 3) is saved to a file and can be recovered from the file
Save value and data type serialization
Restoring data (values and data types) saved in a file is called deserialization
In order for the class to be serializable, we must implement one of these two interfaces
Serializable (the tag interface has no method recommendation +)
Externalizable
ObjectInputStream provides deserialization
ObjectOutputStream provides serialization
2.1ObjectOutputStream
public static void main(String[] args) throws IOException { //serialize String filepath = "d:\\hello.txt"; ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filepath)); //Write a number objectOutputStream.writeInt(100); //Write a floating point number objectOutputStream.writeDouble(5.20); //Write a string objectOutputStream.writeUTF("love"); //Write a Boolean value objectOutputStream.writeBoolean(true); //Write a character objectOutputStream.writeChar('a'); //Write an object objectOutputStream.writeObject(new Dog("floret❀",5)); objectOutputStream.close(); }
2.2 ObjectInputStream
public static void main(String[] args) throws IOException, ClassNotFoundException { String filepath = "d:\\hello.txt"; ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filepath)); System.out.println(objectInputStream.readInt()); System.out.println(objectInputStream.readDouble()); System.out.println(objectInputStream.readUTF()); System.out.println(objectInputStream.readBoolean()); System.out.println(objectInputStream.readChar()); System.out.println(objectInputStream.readObject()); }
Dog.java
public class Dog { private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } }
2.3 precautions
1. The reading and writing order shall be consistent
2. Serializable interface is required for serializing or deserializing object classes
3. It is recommended to add SerialVersionUID to serialized classes to improve version compatibility
private static final long serialVersionUID=1L;
4. When serializing an object, all attributes in it are serialized by default, except for members decorated with static or transient
5. When serializing an object, it is required that the data type of the internal attribute also implement the serialization interface
6. Serialization is inheritable, that is, if a class has implemented serialization, all its subclasses have implemented serialization by default
three Standard input / output stream
3.1System.in
Standard input stream
public static void main(String[] args) throws IOException { InputStream in = System.in; int by; while((by=in.read())!=-1){ System.out.println((char) by); }
3.2System.out
Standard output stream
public static void main(String[] args) throws IOException { PrintStream out = System.out; out.println(5); out.println('a'); }
three point three summary
//Standard input keyboard //Compile type InputStream //Run type BufferedInputStream System.out.println(System.in.getClass()); //Standard output display //Compile type PrintStream //Run type PrintStream System.out.println(System.out.getClass());
four Conversion flow
effect Convert byte stream to character stream Improve character processing efficiency
public static void main(String[] args) throws IOException { //Convert byte stream to character stream String filepath = "d:\\1.txt"; //Convert FileInputStream to InputStreamReader //Specify encoding gbk InputStreamReader gbk = new InputStreamReader(new FileInputStream(filepath), "gbk"); //Pass InputStreamReader into BufferedReader BufferedReader reader = new BufferedReader(gbk); //read String s = reader.readLine(); System.out.println(s); reader.close(); }
five Print stream
5.1 byte print stream
//Byte print stream PrintStream out = System.out; //By default, the location of PrintStream output data is the standard output, that is, the display out.println("hello");//The essence is write out.write("a hh".getBytes()); out.close(); System.setOut(new PrintStream("d:\\1.txt"));//Change the path to output the new file System.out.println("hello Han Shunping anxiety");
5.2 character print stream
public static void main(String[] args) throws IOException { PrintWriter printWriter = new PrintWriter(new FileWriter("d:\\1.txt")); printWriter.print("hi background"); printWriter.close(); }