Basic summary of Flow Java/io
A common code discussion through one line: New BufferedReader (New InputStreamReader (system.in))
Java IO is based on the stream concept, what is a stream, as a beginner,
As I understand it, the bits that are transmitted between applications, which can already be thought of as fluids, can be thought of as currents, so what should be the choice of tools to transfer water between the various sources? In general, water pipes are available, so data I compare data sources to water sources and stream objects to water pipes
So there is the first step of understanding the convection, it is no longer mysterious.
For the flow, we have to study what, we are mainly for the application of the characteristics of the flow, and then according to the characteristics of the flow, we transfer different data, the selection of the flow of the object, to achieve our goal.
Let's analyze the flow from a line of common code!
New BufferedReader (New InputStreamReader (system.in)), which is used to accept one line of input from the keyboard, let's analyze it from the inside out.
The type of system.in is InputStream, which represents the input that the keyboard accepts, that is, the keyboard is the data source, the system.in type can be attributed to node stream, byte stream, input stream, and then inputstreamreader this object is the process flow, character stream , input stream;
The outermost type of bufferedreader is the buffer processing stream, the character stream, and the input stream. is not a bit around Ah, below we start from the classification of the stream.
Classification of streams
(Focus on the classification to remember the appearance of these streams)
Sort by direction:
Input stream and output stream
It is important to note that the input and output of the stream is application-based.
Input stream, looks very good to remember, in general, the input stream is with the word input or reader, such as FileInputStream and BufferedReader, and so on, these are the input stream.
Output stream, in general, with output words or writers, such as FileOutputStream and FileWriter, and so on, please check the API documentation to see if this is the case.
As to when to use the input stream, when to use the output stream, I think we do not have to discuss it!
In accordance with the Units processed:
Byte Stream and character stream
Byte stream, usually with the words of the stream, such as Inputstream,fileinputstream, and so on, this group of flow processing of the smallest unit of bytes.
Character stream, usually with reader or writer words, such as InputStreamReader, and so on, they deal with the smallest unit is the character.
According to the source of the data:
node flow and processing flow
The data source of the node stream is the application, file, keyboard, and so on, non-stream object source, and the data source of processing stream is other stream object.
Use of Streams
I. INPUT and OUTPUT
1.stream represents any data source capable of producing data, or any receiving source capable of receiving data.
In Java IO, all streams (both input and out stream) include two types:
1.1 Byte-oriented stream
A byte-oriented stream that represents reading from the stream in bytes or writing information to the stream. The byte-oriented stream includes the following types:
1) Input stream:
1) Bytearrayinputstream: Use a buffer in memory as a InputStream
2) StringBufferInputStream: Use a String object as a InputStream
3) FileInputStream: To use a file as a inputstream, to achieve the read operation of the file
4) PipedInputStream: realized the concept of pipe, mainly used in the thread
5) Sequenceinputstream: Merge multiple inputstream into one InputStream
2) out stream
1) Bytearrayoutputstream: Storing information in a buffer in memory
2) FileOutputStream: Depositing information into a file
3) PipedOutputStream: realized the concept of pipe, mainly used in the thread
4) Sequenceoutputstream: Merge multiple outstream into one OutStream
1.2 Unicode character-oriented stream
A Unicode-oriented stream that represents a Unicode character that reads from or writes information to a stream. The Unicode character-oriented stream includes 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: No corresponding byte-oriented stream
3) FileWrite: corresponds to FileOutputStream
4) Pipedwrite: corresponds to PipedOutputStream
The character-oriented stream basically has a byte-oriented stream corresponding to it. Two corresponding classes implement the same function, the word is in the direction of the operation is different. such as CharArrayReader: and Bytearrayinputstream's role is to use a buffer in memory as InputStream, the difference is that each time a byte from memory read information, the latter each time from memory read a character.
1.3 Conversion between two non-current-directed streams
InputStreamReader and Outputstreamreader: Converts a byte-oriented stream into a character-oriented stream.
2. Stream Add Property
2.1 Function of "add attribute to stream"
Using the API of operation IO in Java described above, we can do whatever we want to do. But with subclasses of FilterInputStream and Filteroutstream, we can add properties to the stream. Below with a
Example to illustrate the role of this function.
If we are going to write data to a file, we can do this:
Fileoutstream fs = new Fileoutstream ("test.txt");
The write () function can then be called by the resulting FS object to the data in the Test.txt file. However, if we want to implement the "first to write the data to the file into memory, and then write the cached data into the file" function, the above API will not be able to meet our needs. But with FilterInputStream and Filteroutstream subclasses, add the functionality we need for fileoutstream.
Various types of 2.2 FilterInputStream
The 2.2.1 is used to encapsulate byte-oriented InputStream
1) DataInputStream: reads the base type (int, char, etc.) data from the stream.
2) Bufferedinputstream: Using buffers
3) Linenumberinputstream: The number of rows in the input stream is recorded and can then be called Getlinenumber () and setlinenumber (int)
4) Pushbackinputstream: Rarely used, generally used for compiler development
The 2.2.2 is used to encapsulate character-oriented InputStream
1) There is no class corresponding to the DataInputStream. Use DataInputStream unless you want to use ReadLine () instead of BufferedReader
2) BufferedReader: corresponds to Bufferedinputstream
3) LineNumberReader: corresponds to Linenumberinputstream
4) Pushbackreader: corresponds to Pushbackinputstream
Various types of 2.3 filteroutstream
The 2.2.3 is used to encapsulate byte-oriented outputstream
1) Dataioutstream: Outputs the basic type (int, char, etc.) data to stream.
2) Bufferedoutstream: Using buffers
3) PrintStream: produces formatted output
The 2.2.4 is used to encapsulate character-oriented OutputStream
1) Bufferedwrite: with corresponding
2) Printwrite: with corresponding
3. Randomaccessfile
1) Read and write files can be completed by Randomaccessfile object
2) When generating an object, you can indicate the nature of the file to be opened: R, read only, W, write only, rw readable and writable
3) You can jump directly to the location specified in the file
4. An example of an I/O application
Import java.io.*;
public class testio{
public static void Main (string[] args)
Throws ioexception{
1. Read data from a file in a behavior unit
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. Receiving input from the keyboard
BufferedReader stdin =
New BufferedReader (
New InputStreamReader (system.in));
System.out.println ("Enter a Line:");
System.out.println (Stdin.readline ());
2. Reading data from a string object
StringReader in2 = new StringReader (s2);
int C;
while ((c = In2.read ())! =-1)
System.out.println ((char) c);
In2.close ();
3. Out of memory format input
try{
DataInputStream in3 =
New DataInputStream (
New Bytearrayinputstream (S2.getbytes ()));
while (true)
System.out.println ((char) in3.readbyte ());
}
catch (Eofexception e) {
System.out.println ("End of Stream");
}
4. Output to File
try{
BufferedReader in4 = new BufferedReader (new StringReader (S2));
PrintWriter OUT1 =
New PrintWriter (
New BufferedWriter (
New FileWriter ("f://nepalon//testio.out"));
int linecount = 1;
while ((s = in4.readline ()) = null)
Out1.println (linecount++ + ":" + s);
Out1.close ();
In4.close ();
}
catch (Eofexception ex) {
System.out.println ("End of Stream");
}
5. Storage and recovery of data
try{
DataOutputStream Out2 =
New DataOutputStream (
New Bufferedoutputstream (
New FileOutputStream ("f://nepalon//Data.txt"));
Out2.writedouble (3.1415926);
Out2.writechars ("/nthas was pi:writechars/n");
Out2.writebytes ("THAs was pi:writebyte/n");
Out2.close ();
DataInputStream In5 =
New DataInputStream (
New Bufferedinputstream (
New FileInputStream ("f://nepalon//Data.txt"));
BufferedReader in5br =
New BufferedReader (
New InputStreamReader (IN5));
System.out.println (In5.readdouble ());
System.out.println (In5br.readline ());
System.out.println (In5br.readline ());
}
catch (Eofexception e) {
System.out.println ("End of Stream");
}
6. Manipulating files via Randomaccessfile
Randomaccessfile RF =
New Randomaccessfile ("f://nepalon//rtest.dat", "RW");
for (int i=0; i<; i++)
Rf.writedouble (i*1.414);
Rf.close ();
RF = new Randomaccessfile ("f://nepalon//rtest.dat", "R");
for (int i=0; i<; i++)
System.out.println ("Value" + i + ":" + rf.readdouble ());
Rf.close ();
RF = new Randomaccessfile ("f://nepalon//rtest.dat", "RW");
Rf.seek (5*8);
Rf.writedouble (47.0001);
Rf.close ();
RF = new Randomaccessfile ("f://nepalon//rtest.dat", "R");
for (int i=0; i<; i++)
System.out.println ("Value" + i + ":" + rf.readdouble ());
Rf.close ();
}
}
Explanation of the code (in extents):
In zone 1, when a file is read, the contents of the file are read into the cache, and when In.readline () is called, the data is read from the cache as a character ("cache bytes read").
In 1b, because you want to read data from a standard IO (keyboard) in cache byte reads, you first convert the standard IO (system.in) to a character-directed stream, and then the BufferedReader package.
In zone 2, to read data from a string object as a character, a stream of type StringReader is generated.
In zone 4, when the data is read by the string Object s2, the data in the object is stored in the cache and then read from the buffer, and when the Testio.out file is manipulated, the formatted information is output to the cache, and the information in the cache is output to the file.
In zone 5, when outputting the Data.txt file, the basic type of data is first output to the house cache, and then the data in the cache is output to a file; When the file is read, the data in the file is read into the cache and then read from the cache in the form of a basic type. Note the in5.readdouble () line. Because the first writedouble () is written, it is intended to be displayed correctly. It should also be read in the form of a basic type.
The 6 zone operates on files through the Randomaccessfile class.
New BufferedReader in Java (New InputStreamReader (system.in))