One of Java's Io operations: Basic Concepts

Source: Internet
Author: User
Tags object serialization

Convenient Io operations are required for every successful language, but it is not easy to have a good Io library, because there are not only three different types of Io to consider (file, console, network connection ), in addition, you need to communicate with them in a large number of different ways (sequence, random access, binary, character, by line, by word, etc ).
Serialization is a mechanism for processing object streams. Object streams are called object streams that stream object content. You can perform read and write operations on Streaming objects, or transfer streaming objects between networks. The most important use of Object serialization is to ensure Object Integrity and portability when passing and saving objects.

The IO class of the Java library is divided into two parts: Input and Output. by inheritance, all classes derived from inputstream (input stream) have the basic method named read, read a single byte or byte array. Similarly, all classes derived from outputstream have the basic write () method, which is used to write a single byte or byte array. However, we usually do not use these methods; they exist because more complex classes can use them to provide a more useful interface. It is necessary to classify classes by function. The Database Designer first decides that all input-related classes are inherited from inputstream, and all output-related classes are inherited from outputstream.
1. inputstream type
(1) bytearrayinputstream (byte array ). Allows a buffer in the memory to be used as inputstream. Buffer from which bytes are extracted/used as a data source. A useful interface can be provided by connecting the same filterinputstream object.
(2) stringbufferinputstream (String object ). Converts a string to an inputstream string ). The basic implementation scheme actually uses a stringbuffer (string buffer)/as a data source. A useful interface can be provided by connecting the same filterinputstream object.
(3) file. Fileinputstream is used to read information from a file. Represents a string of the file name, or a file or filedescriptor object/used as a data source. A useful interface can be provided by connecting the same filterinputstream object.

2. Type of outputstream
(1). bytearrayoutputstream creates a buffer in the memory. All the data we send to the stream is placed in this buffer zone. The initial size of the optional buffer/used to indicate the destination of the data. If you connect it with a filteroutputstream object, you can provide a useful interface.
(2). fileoutputstream sends information to a file. A string is used to represent the file name, or a file or filedescriptor object/is used to indicate the data destination. If you connect it with a filteroutputstream object, you can provide a useful interface.

3. filterinputstream type
(1). datainputstream, used with dataoutputstream, can read the basic data type from the stream according to the portable mode, and its constructor parameter is inputstream;
(2) bufferedinputstream, which can prevent the actual write operation from each read operation. It indicates that "using the buffer", and the constructor parameter is still inputstream;

4. filteroutputstream type
(1). dataoutputstream .....
(2) printstream is used to generate formatted output, where dataoutputstream stores and printstream processes and displays data. The constructor parameter is outputstream;
(3). bufferedoutputstream .....

5. Reader and writer (providing Unicode-compatible and character-oriented Io functions)
(1). to associate the "Byte" hierarchy with the "character" hierarchy, an adapter is required. Inputstreamreader can convert inputstream to reader,
Outputstreamreader can convert outputstream to writer
(2). fileinputstream corresponds to filereader; fileoutputstream corresponds to filewriter; stringbufferinputstream corresponds to stringreader;
(3) filterinputstream: filterreader; filteroutputstream: filterwriter; bufferedinputstream: bufferedreader; bufferedoutputstream: bufferedwriter; printstream: printwriter; streamtokenizer: streamtokenizer

6. General steps for operating Java I/O:
(1) first create the data source Io,
(2) create an I/O class to be packaged as needed. The constructor parameter is the created data source I/O;
(3). Io handle obtained by the operation;
Example program: create a buffer file input stream. Assume that you need to read the file "E: // Java/data.txt" from the disk ":
// Create a fileinputstream:
Fileinputstream fileinput = new fileinputstream ("E: // Java/data.txt ");
// Create a bufferedinputstream:
Bufferedinputstream bufferedinput = new bufferedinputstream (fileinput );
// The obtained bufferedinput is a buffer file input stream, or its name can be further abbreviated as follows:
Bufferedinputstream input = new bufferedinputstream (New fileinputstream ("E: // Java/data.txt "));
// The input obtained now is a buffer file input stream.

 
Http://blog.csdn.net/Cpp_Java_Man/archive/2006/05/02/705271.aspx ()

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.