The input and output functions of Java language are very powerful and flexible, and the input and output operations of data are carried out in a "stream" way. J2SDK provides a variety of "stream" classes to obtain different types of data, defined in the package java.io. Data is input or output in the program through standard methods.
Streams in Java can be classified from different perspectives:
According to the direction of the stream: it is divided into input stream and output stream.
According to the different data units: it is divided into byte stream (8 bits) and character stream (16 bits).
According to different functions: divided into node flow and processing flow.
Node flow: is a flow (such as file, memory) that can read and write data from a specific data source (node). It's like a single tube connected to the faucet and starting to release water.
Processing flow: It is "connected" on an existing stream (node flow or processing flow), providing the program with more powerful read and write functions through processing data. Just like on the basis of having a pipe (node flow) connected, a few thicker and special-functional pipes (processing flow) are put on further treatment of the outflow water.
All stream types provided by J2SDK that are located in the package java.io inherit the following four abstract stream types.
The four basic streams InputStream, OutputStream, Reader, and Writer have more specific subclasses, which are divided into file streams, buffer streams, data streams, conversion streams, Print streams, Object streams, etc., all of which have specific functions or are used to operate specific data. Among them, dark colors represent node flow, and light colors represent processing flow.
Here we summarize the specific usage of flow through node flow and processing flow.
Classification of node flows:
Node streams process File (file), Array (array in memory), String (string), and Pipe (pipe) respectively.
Classification of processing flows:
Commonly used processing streams include: buffer stream, conversion stream, data stream, object stream, and print stream.
Buffered stream: It needs to be socketed on the corresponding byte stream, and provides buffering function for read and written data, which improves read and write efficiency, and also adds some more convenient methods.
The function of a buffer flow is like a bucket. The original byte flow is a water pipe. The water pipe is directly connected to the destination. After adding the buffer flow, a bucket is connected under the water pipe and then poured into the destination after the bucket is filled with water. Plays a buffering role. This way you can read more data at once, avoiding frequent reading and writing to the hard disk. Since it has a buffering function, you need to use the flush method when writing data.
import java.io.*; public class TestBufferStream{ public static void main(String[] args){ try{ BufferedWriter bw=new BufferedWriter(new FileWriter("F://Java//IO/dat.txt"));//Buffered stream on the byte stream on the written file BufferedReader br=new BufferedReader(new FileReader("F://Java//IO/dat.txt"));//Buffered stream on the byte stream on the read file String s=null; for(int i=1;i<=100;i++){ s=String.valueOf(Math.random());// Assign s to bw.write(s); //Write s to dat.txt file bw.newLine(); //Write a line break character. A better way to use buffered streams is to write or read a row of data. } bw.flush(); //Make all data in memory be written out immediately and no longer buffered. while((s=br.readLine())!=null){ //Read the file content by line System.out.println(s); } bw.close(); //Close the processing flow and close the node flow inside. br.close(); } catch(IOException e){ e.printStackTrace(); } } } Convert flow: Use conversion with byte data to character data. The InputStreamReader requires an InputStream socket, and the OutputStreamWriter requires an OutputStream socket.
Data flow: DataInputStream and DataOutputStream provide writing or reading the basic data type into a file. This stream is of great use. If there is no such stream, there is a long that only takes up 8 bytes. If I want to write to the file, I need to convert it into a string and then convert it into a character array, the space will take up a lot. However, with this stream, it is very convenient. Just write these 8 bytes to the file, which not only saves memory space but also makes the program more convenient and simple. However, you need to pay attention when reading. According to the type of data you read, the pointer will move downward, so the order you read must be consistent with the order of writing to achieve your correct needs. Otherwise it is equivalent to splitting the data.
import java.io.*; public class TestDataStream{ public static void main(String[] args){ ByteArrayOutputStream baos=new ByteArrayOutputStream(); // Create a node flow. DataOutputStream dos=new DataOutputStream(baos); //Connect a data stream "pipe" on the node stream try{ dos.writeDouble(Math.random()); //double type, 8 bytes dos.writeBoolean(true); //boolean type, occupies one byteByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray()); System.out.println(bais.available()); //Total number of available bytes in the output stream--9 DataInputStream dis=new DataInputStream(bais); //Also connect the data stream "pipe" outside the output stream System.out.println(dis.readDouble()); //Turnly read out the double type number System.out.println(dis.readBoolean()); //Turnly read out the boolean type dos.close(); dis.close(); }catch(IOException e){ e.printStackTrace(); } } }
Print stream: java.io provides streams for printing. This type of stream has the function of automatically flushing when writing, so there is no need to flush every time you print something.
import java.io.*; public class TestPrintStream1{ public static void main(String[] args){ PrintStream ps=null; try{ FileOutputStream fos=new FileOutputStream("F://Java//IO/log.dat");//Create the output stream and specify the output location ps=new PrintStream(fos); //Package the print stream outside the stream}catch(IOException e){ e.printStackTrace(); } if(ps!=null){ System.setOut(ps); //Set the system's printing to print stream ps } int ln=0; for(char c=0;c<=60000;c++){ System.out.print(c+" "); //Not printing to the dos window, the output stream is printed directly into the specified file if(ln++>=100){ System.out.println();ln=0; } } } }