Java enhanced course summary! July February 20!

Source: Internet
Author: User

I reviewed the I/O knowledge based on the video these two days. Although I have a Java Foundation, I/O is too cumbersome, so I read it carefully, then, based on the content in the video, I found some information on the Internet and summarized the I/O knowledge!

The concept of stream is derived from the concept of pipe in UNIX. In UNIX, a media transcoding queue is an uninterrupted byte stream used for communication between programs or processes, or reading and writing peripheral devices and external files.
A stream must be a source end and a destination end. It can be a certain area of computer memory, a disk file, or even a URL on the Internet.
The direction of the stream is important. Based on the direction of the stream, the stream can be divided into two types: input stream and output stream. You can read information from the input stream but cannot write it. On the contrary, the output stream can only be written into the input stream, but cannot be read. In fact, the input/output is intended for memory.
In fact, the source and destination of a stream can be viewed as producers and consumers of bytes. For an input stream, you do not have to worry about its source, as long as data is read from the stream, but the output stream does not know its target end, it simply writes data to the stream.
Java. the classes in the IO package correspond to two types of streams. A type of stream reads or writes directly from a specified location (such as a disk file or memory area). Such streams are called node streams, other streams are called filtering streams (Packaging streams)
Filter stream: Some streams can receive bytes from files and other places, while others can combine bytes into more useful data types. A constructor that passes an existing stream to another stream. The two streams are combined and the combined stream is called a filter stream.
The input stream of the filter is often used as the input source of another input stream. After filtering or processing, it is provided to the user in the form of a new input stream. The output stream of the filter is also similar.

Common JAVA input and output streams
In fact, they are all inherited from four abstract classes, which are
Single-byte-based inputstream and outputstream
Reader and writer based on double-byte Unicode code units
Once the input stream is opened, the program can read data from the input stream in sequence.
The process of reading data from an input stream is generally as follows:
Open a stream
While more information
Read Information
Close the stream

Similarly, the program can send information to the target end by opening an output stream and writing data in sequence.
The process of writing data to the output stream is generally as follows:
Open a stream
While more information
Write Information
Close the stream
The stream classes in the Java. Io package can be divided into two categories based on the type of the objects they operate on, namely character or byte: two types of stream and word throttling.
The inputstream and outputstream classes only read and write a single byte and byte array. They do not have methods to read and write strings and values.
Because it is inconvenient to store the Unicode code in bytes (each code unit in Unicode uses two bytes ), therefore, we have a special class hierarchy to process Unicode characters. These classes inherit from the abstract classes reader and writer.

1.1 byte-oriented stream

A byte-oriented stream that reads or writes information to a stream in bytes. Byte-oriented streams include the following types:
1. Input stream:
1) bytearrayinputstream: uses a buffer in the memory as an inputstream.
2) stringbufferinputstream: Use a String object as inputstream --- expired. This class fails to correctly convert characters to bytes. From JDK 1.1, the preferred method to create a stream from a string is to create a stream through the stringreader class.
3) fileinputstream: uses a file as inputstream to read the file.
4) pipedinputstream: implements the pipe concept and is mainly used in the process.
5) sequenceinputstream: combines multiple inputstreams into one inputstream.
2. Out stream
1) bytearrayoutputstream: stores information in a buffer zone in the memory.
2) fileoutputstream: stores information in a file.
3) pipedoutputstream: implements the concept of pipe, which is mainly used in the process.
4) sequenceoutputstream: combines multiple outstreams into one outstream.
1.2 Unicode Character-oriented stream
A Unicode-oriented stream that reads or writes information from a stream in units of Unicode characters. Unicode-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: no corresponding byte-oriented stream
3) filewrite: corresponds to fileoutputstream
4) pipedwrite: corresponds to pipedoutputstream
Character-oriented stream basically corresponds to a byte-oriented stream. The functions of the two corresponding classes are the same, and the words are different in operation. For example, chararrayreader and bytearrayinputstream both use a buffer in the memory as inputstream. The difference is that the former reads a byte of information from the memory each time, the latter reads one character from the memory each time.

1.3 conversions between two unrestricted stream types
Inputstreamreader and outputstreamreader: convert a byte-oriented stream into a character-oriented stream.
Inputstreamreader is a bridge between byte stream and byte stream: It reads bytes using the specified charset and decodes them into characters. Its character set can be specified by name or explicitly specified, or it can accept the default Character Set of the platform.
Outputstreamwriter is a bridge between the bytes stream and the byte stream. You can use the specified charset to encode the characters in the stream into bytes. The character set used by the platform can be specified or explicitly specified by the name. Otherwise, the default Character Set of the platform will be accepted.

2. Add attributes to stream
2.1 role of "adding attributes for stream"
Using the I/O APIs in Java described above, we can complete any operation we want to complete. However, through the sub-classes of filterinputstream and filteroutstream, we can add attributes for stream. The following is an example to illustrate the role of this function.
If we want to write data to a file, we can do this:
Fileoutstream FS = new fileoutstream(‑test.txt ");
Then, you can use write(writable writable) to write data to the test.txt file. However, if we want to implement the "First cache the data to be written into the file into the memory, then write the data in the cache into the file" function, none of the above APIs can meet our needs. However, we can use the sub-classes of filterinputstream and filteroutstream to add the required functions for fileoutstream.

2.2 various types of filterinputstream
2.2.1 encapsulate byte-oriented inputstream
1) datainputstream: reads data of basic types (such as int and char) from stream.
2) bufferedinputstream: Buffer
3) linenumberinputstream: records the number of rows in the input stream. You can call getlinenumber () and setlinenumber (INT)
4) pushbackinputstream: rarely used and generally used for Compiler Development
2.2.2 encapsulate character-oriented inputstream
1) there is no class corresponding to datainputstream. Unless you use bufferedreader 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 encapsulate byte-oriented outputstream
1) dataioutstream: outputs basic data types (such as int and char) to stream.
2) bufferedoutstream: Buffer
3) printstream: produce formatted output
2.2.4 character-oriented outputstream Encapsulation
1) bufferedwrite: corresponds to bufferedoutstream
2) printwrite: corresponds to printstream
3. randomaccessfile
1) You can use the randomaccessfile object to read and write files.
2) when an object is generated, you can specify the nature of the file to be opened: R, read-only; W, write-only; RW, read/write
3) You can directly jump to the specified position 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 behavior units
/* When reading a file, read the file content to the cache first. When in. Readline () is called,
Then read data from the cache in character format (hereinafter referred to as "cache byte read mode ").
*/
Bufferedreader in = new bufferedreader (New filereader ("F: // Java // testio. Java "));
String S, S2 = new string ();
While (S = in. Readline ())! = NULL)
S2 + = S + "/N ";
In. Close ();

// 1b. Receive keyboard input
/* To read data from the standard I/O (keyboard) in the cache byte mode
First, convert the standard io (system. In) to a character-oriented stream, and then encapsulate bufferedreader.
*/
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
/*
To read data from a string object in the form of characters, a stringreader stream is generated.
*/
Stringreader in2 = new stringreader (S2 );
Int C;
While (C = in2.read ())! =-1)
System. Out. println (char) C );
In2.close ();

// 3. Extract formatted input from memory
// Use a buffer in the memory as datainputstream

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
/* When reading data from String object S2, the data in the object is first stored in the cache and then read from the buffer. When operating on the testio. Out file,
First, the formatted Information is output to the cache, and then the cached information is output to the file.
*/
Try {
Bufferedreader in4 = new bufferedreader (New stringreader (S2 ));
Printwriter out1 = new printwriter (New bufferedwriter (New filewriter ("F: // Java // testio. Out ")));
Int linecount = 1;
While (S = in4.readline ())! = NULL)
Out1.println (linecount ++ ":" + S );
Out1.close ();
In4.close ();
} Catch (eofexception ex ){
Ystem. Out. println ("End of stream ");
}

// 5. Data Storage and Restoration
/* When outputting the data.txt file, the basic data type is first output to the House cache, and then the cached data is output to the file. when reading the file, first, read the data in the file to the cache, and then read the data in the basic type from the cache. Note the in5.readdouble () line. Because the first writedouble () is written, it must be correctly displayed. It also needs to be read in the form of basic types.
*/
Try {
DataOutputStream out2 = new DataOutputStream (new BufferedOutputStream (new FileOutputStream ("F: // java // 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: // java // 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. Operate the file through RandomAccessFile
// Use the RandomAccessFile class to operate the file.
Randomaccessfile Rf = new randomaccessfile ("F: // Java // rtest. dat", "RW ");
For (INT I = 0; I <10; I ++)
RF. writedouble (I * 1.414 );
RF. Close ();

Rf = new randomaccessfile ("F: // Java // rtest. dat", "R ");
For (INT I = 0; I <10; I ++)
System. Out. println ("value" + I + ":" + RF. readdouble ());
RF. Close ();

Rf = new randomaccessfile ("F: // Java // rtest. dat", "RW ");
RF. Seek (5*8 );
RF. writedouble (47.0001 );
RF. Close ();

Rf = new randomaccessfile ("F: // Java // rtest. dat", "R ");
For (INT I = 0; I <10; I ++)
Ystem. Out. println ("value" + I + ":" + RF. readdouble ());
RF. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.