Java io stream learning and Java io stream
I have carefully studied Java I/O streams in recent days. I was planning to watch videos and learn through videos. However, I found that videos are not easy to understand, so I learned the Java io stream through Baidu and api documentation.
Io streams can be classified into two types: byte streams and byte streams, and input streams and output streams.
Let's talk about byte streams and byte streams first: in fact, byte streams do not use the buffer during operations, but are directly operated by the file itself, however, the swap stream uses the buffer when operating, and uses the buffer to operate files. The other difference between byte stream and byte stream is that the Read and Write units are different. Undoubtedly, the byte stream is in character units, and the byte stream is in bytes (8 bits per byte ), the two types of processing objects are different. byte streams can process all types of data, while character streams can only process character-type data. Therefore, we can draw a conclusion that as long as the plain text data is processed, the byte stream is preferred.
Then let's talk about the input stream and output stream. To be honest, I have input and output since I learned C language. The output and input are very messy and hard to understand. The difference must be kept in mind after constant contact. Input generally refers to obtaining data from external files (such as hard disks and keyboards), and output refers to outputting data from the program itself to the outside (such as writing files, display All on the screen ). Another difference between an input stream and an output stream is that the input stream can only be read, while the output stream can only be written.
With these two categories, let's take a look at some of the details.
As shown in the inheritance diagram, character input stream Reader is the parent class of all character input streams. PIpedReader can read data through pipelines built with other threads.
InputStreamReader is a bridge between the byte stream and the primary stream. It can convert byte streams into the primary stream.
Character output stream Writer is the parent class of all character output streams. The other child classes correspond to the Child classes of Reader respectively.
InputStream is the parent class of all input byte streams. ByteArrayInputStream, StringBufferInputStream, and FileInputStream read data from the corresponding files respectively.
PipedInputStream reads data from the pipeline shared with other threads.
Output byte stream OutputSteam is the parent class of all output byte streams. The others correspond to the subclass of InputSteam respectively.
In addition to byte streams and character streams, the entire IO class also includes byte and character conversion streams.
OutputStreramWriter converts the output bytes stream to a byte stream.
InputStreamReader switches the input bytes to the bytes stream.
Convert byte output to character output stream Writer out =
new
OutputStreamWriter(
new
FileOutputStream(file));
Convert the byte input stream to the character input stream Reader read =
new
InputStreamReader(
new
FileInputStream(file));
There is a problem where conversion streams are used only when conversion operations are performed between byte and character, and data in stream operations needs to be encoded or decoded.
Take the content as the output input destination, and use the memory operation stream ByteArrayInputStream to write the content into the content ByteArrayOutputStream. The operation stream of the content output from the memory is generally used to generate some temporary information, this avoids the trouble of deleting data.
Merge stream SequenceInputStream is mainly used to merge two streams, for example, merge the content of two txt files into another txt file.
PushBackInputStream: You can return some of the read data to the data buffer.
BufferedReader can only accept the buffer of the bytes stream. Because each Chinese Character occupies two bytes, you need to change the System. in byte input stream to the character input stream, using:
BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); |
Usually, the sequence class is used for data input. Sequence sca =
new
Scanner(System.in);
// Read an integer
int
temp = sca.nextInt();
I checked the api documentation and found that the callback parameter indicates where the data is scanned. The callback can accept any input stream.
Redirection output for System. out. println () (you can change the output to the console to write files) System. err redirection (you can write error information to a file and save it)
It is often used in stream operations: And \ is not recommended to be used directly. It is best to call two constants File. separator (\) and File. pathSeparator (:). In this way, cross-platform operations will be more compatible.
To wrap a line in a file, use "\ r \ n"
How should I learn the java io stream chapter? Very
There are two streams and byte streams.
Streams are generally used to process strings, such as reading by row, replacing and comparing read strings.
Byte streams are generally simple reading or writing files.
To be specific to every detail, we need to accumulate experience, which is commonly used.
The primary stream is BufferedWriter and BufferedReader.
Byte streams are FileInputStream, FileOutputStream, DataInputStream, and DataOutputStream.
If you have any questions, continue.
Java io stream details
I think the main idea is too much...
It is easy to remember the input and output streams... this is what I remember when I learned the input and output streams:
Input stream: Read (from a file or system)
Output stream: Write (to file or memory)
What does the landlord think of this ???
Is that Understandable ??? Is to understand in the opposite direction... it is convenient and practical ....
Haha ....
Wish you an early success!
OutputStream is the output stream... its method write () is of course to write the parameters in brackets to the specified file! That's right!
Write (int B). This function writes an integer B to a specified file.
If you have any questions, please contact me...