Home >Java >javaTutorial >Java language goes deep into the essence of JAVA IO flow
1.Input and Output
1.stream represents any data source capable of producing data, or any receiving source capable of receiving data.
In Java's IO, all streams (including Input and Out streams) include two types:
1.1 Byte-oriented stream
Byte-oriented stream means that bytes are extracted from the stream Read or write information to the stream. Byte-oriented streams include the following types:
1) input stream:
1) ByteArrayInputStream: use a buffer in memory as an InputStream
2) StringBufferInputStream: use a String object as an InputStream
3) FileInputStream: Use a file as an InputStream to realize the reading operation of the file
4) PipedInputStream: implements the concept of pipe, mainly used in threads
5) SequenceInputStream: merges multiple InputStreams into one InputStream
2) Out stream
1) ByteArrayOutputStream: Store information in a buffer in memory
2) FileOutputStream: Store information in a file
3) PipedOutputStream: Implement the concept of pipe, mainly used in threads
4) SequenceOutputStream: Merge multiple OutStreams It is an OutStream
1.2 Unicode character-oriented stream
Unicode character-oriented stream, which means reading or writing information from the stream in units of Unicode characters. Unicode character-oriented streams include the following types:
1) Input Stream
1) CharArrayReader: corresponds to ByteArrayInputStream
2) StringReader: corresponds to StringBufferInputStream
3) FileReader: corresponds to FileInputStream
4) PipedReader: corresponds to PipedInputStream
2) Out Stream
1) CharArrayWrite: Corresponds to ByteArrayOutputStream
2) StringWrite: There is no corresponding byte-oriented stream
3) FileWrite: Corresponds to FileOutputStream
4) PipedWrite: Corresponds to PipedOutputStream
Character-based A oriented stream basically has a corresponding byte-oriented stream. The functions implemented by the two corresponding classes are the same, but the guidance during operation is different. For example, CharArrayReader: and ByteArrayInputStream both use a buffer in memory as an InputStream. The difference is that the former reads one byte of information from memory each time, while the latter reads one character from memory each time.
1.3 Conversion between two non-currently oriented streams
InputStreamReader and OutputStreamReader: Convert a byte-oriented stream into a character-oriented stream.
2. Add attributes to stream
2.1 The role of "add attributes to stream"
Using the API for operating IO in Java introduced above, we can complete any operation we want to complete. But through subclasses of FilterInputStream and FilterOutStream, we can add properties to the stream. Below is an
example to illustrate the function of this function.
If we want to write data to a file, we can do this:
FileOutStream fs = new FileOutStream("test.txt");
Then we can call the write() function to and from the test.txt file through the generated fs object Data is written into it. However, if we want to implement the function of "first caching the data to be written to the file into the memory, and then writing the data in the cache to the file", none of the above APIs can meet our needs. But through subclasses of FilterInputStream and FilterOutStream, we can add the functions we need to FileOutStream.
2.2 Various types of FilterInputStream
2.2.1 Used to encapsulate byte-oriented InputStream
1) DataInputStream: Read basic type (int, char, etc.) data from the stream.
2) BufferedInputStream: Use buffer
3) LineNumberInputStream: It will record the number of lines in the input stream, and then you can call getLineNumber() and setLineNumber(int)
4) PushbackInputStream: Rarely used, generally used for compiler development
2.2.2 Used to encapsulate character-oriented InputStream
1) There is no class corresponding to DataInputStream. Unless you use BufferedReader instead when using readLine(), use DataInputStream
2) BufferedReader: corresponds to BufferedInputStream
3) LineNumberReader: corresponds to LineNumberInputStream
4) PushBackReader: corresponds to PushbackInputStream
2.3 Various types of FilterOutStream
2.2.3 Used to encapsulate byte-oriented OutputStream
1) DataIOutStream: Output basic type (int, char, etc.) data into the stream.
2) BufferedOutStream: Use buffer
3) PRintStream: Generate formatted output
2.2.4 Used to encapsulate character-oriented OutputStream
1) BufferedWrite: Corresponds to
2) PrintWrite: Corresponds to
3. RandomaccessFile
1 ) Read and write files can be completed through the RandomAccessFile object
2) When generating an object, you can specify the nature of the file to be opened: r, read-only; w, write-only; rw can be read and written
3) You can jump directly to the location specified in the file
4. An example of I/O application
import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1. Read from a file in row units Data
BufferedReader in =
new BufferedReader(
new FileReader("F:\nepalon\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) ! = null)
s2 += s + "n";
in.close();
//1b. Receive keyboard input
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System .out.println("Enter a line:");
System.out.println(stdin.readLine());
//2. Read data from a String object
StringReader in2 = new StringReader(s2) ;
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();
The above is Java, IO Streaming content, please pay attention to the PHP Chinese website (www.php.cn) for more related articles!