Java Io stream Summary

Source: Internet
Author: User
Callback (also called callback): it can be understood as an interface that provides some methods for other classes, but when other classes call it, it also calls the conditions (rewriting) that other classes give it ).

A Single Random Access File class: The rondomaccessfile class allows random access to files with the ability to read and write at the same time.
The method is close (), read (), writer (), seek (), and getfilepointer (). Note that this is the premise of seek.
Stream:
I. I/O Stream (how Java can communicate with external data)
Stream definition:
Any data source object that can generate data or receive data. It shields the actual I/O device from processing data details.
1. Input/Output: refers to the process that spans the JVM boundary and exchanges data with the source or target data source of external data.

2. stream classification:
Input streams and output streams are classified by flow direction;
Streams are divided into two types by transmission unit: reader and writer );
The function can also be divided into node streams and filter streams.
Node stream: establishes connections between data sources and programs (equivalent to a bare gun)
Filter stream: Used to add a function to a node. (Equivalent to functional parts)
The construction method of the filter stream is constructed by other stream bit parameters (such a design pattern is called the decoration pattern ).
Note: an I/O Stream is a type of valuable resource. after use, you must call the close () method to close the stream and release the resource. When closing a stream, only the outermost layer of the stream is closed.
3. The file class (Java. Io. *) can represent a file or a directory.
(Both files and directories in Java belong to this class, and the distinction is not very obvious.
4. The method in Java. Io is to perform disk operations on files on the disk, but the file content cannot be read.
Note: creating a file object and creating a file are two different concepts in Java. The former creates a file in the virtual machine,
But it is not actually created into the OS file system. With the VM closed, the created object will disappear.
Creating a file is actually creating a file in the system.

Ii. File class: It seems like a file. In fact, it can also refer to a file set. When used as a file set, we can call the list method.
Common structures of this class include: file ("path"), file ("front path", "Back Path") file (file, "path ")
Common methods of this class: exists (), delete (), getname (), getpath (), isdirectory (), isfile (), length (),
Listfile (filefilter) is mainly used to filter files (which can be used to nest internal classes)
Mkdir (), mkdirs (), tostring (),
The filefilter interface only has the accept method, and its return value is boolean type. You can write regular expressions to filter files.
It should be noted that the input accept parameter must be of the final type (the requirements of anonymous internal classes) so that it can use a team image out of the scope of this class.
By the way: Internal classes have the advantages of high aggregation. The disadvantage is that they are not easy to read and are used with caution.
Three-byte stream:
The parent class of all input streams of inputstream/outputstream, which is an abstract class.
Sub-classes: fileinputstream/fileoutputstream (node stream) (Note: fileinputstream (path name, Boolean) Boolean is true, indicating splicing)
Datainputstream/dataoutputstream data input stream allows applications to "machine-independent"
Read the Basic Java data types from the underlying input stream (8 basic types, plus a string). The types must be consistent when reading and writing data.
Bufferedinputstream/bufferedoutputstream byte buffer stream is used to add a buffer function to the node stream. (Typical sacrifice space for Time)
1. byte input stream: inputstream In the IO package is the parent class of all byte input streams.
Int read (); read one byte (one at a time );
You can use the new byte [] = array to call read (byte [] B)
The returned value of read (byte []) can indicate the number of valid values. The returned value of read (byte []) is-1, indicating the end.
2. The close () method in the stream is controlled by the programmer. Because the input and output streams have exceeded the JVM boundary, resources may not be reclaimed.
Principle: all resources that span the Virtual Machine boundary must be shut down by the programmer and do not expect garbage collection.
Iv. character encoding of byte streams:
Character encoding converts a character into a number and stores it in a computer. It maps letters into integers according to ASCII.
The process of converting a number from a computer to a corresponding character is called decoding.
The root cause of Garbled text is the inconsistent encoding/decoding methods. It is compatible with ASCII codes in any encoding method in the world. No garbled characters are found in English.
Classification of encoding methods:
ASCII (numbers, English): 1 character occupies one byte (all the character sets are compatible with ASCII)
ISO8859-1 (Europe): 1 character occupies one byte
GB-2312/GBK: 1 character occupies two bytes. GB represents the national standard.
GBK is a new kind of encoding method added in the GB-2312, it is also the most commonly used Chinese character encoding method.
UNICODE: one character occupies two bytes (slow network transmission)
UTF-8: variable length bytes, for an English byte, Chinese characters three bytes.
Principle: ensure the consistency of codec methods to avoid errors.
I/O learning has two common errors: 1. I forgot to add flush2. no line breaks.
5. Ghost stream
Parent class of all the streams in Reader/writer
Its subclass has
Inputstreamreader/outputstreamwriter is a bridge conversion class that streams data from bytes to bytes. This class can set the character conversion mode
The combination of filereader/filewriter (fileinputstream/fileoutputstream and inputstreamreader/outputstreamwriter) can only be the local default encoding
Bufferedreader/bufferedwriter Character Buffer stream. Its special method Readline () reads a text line. newline () indicates that the writer goes to the next text line.
Sat.
1. Object serialization
1. Definition: the process of writing an object to a file (persistent medium) through an I/O Stream is called object serialization.
2. serialization interface: serializable
This interface does not have any methods. Such an interface is called a tag interface.
3. Not all objects can be serialized. Its instance objects can be serialized only when serializable classes are implemented.
4. define a set of serialization specifications in Java. The encoding and decoding methods of objects have been defined.
5. Class objectoutputstream and objectinputstream are also filter streams, so that the node stream can directly obtain the output object.
Ganbin@tarena.com.cn
The most useful method is:
(1) writeobject (object B)
(2) readobject (); this method returns an object to be read, but note that this method does not return NULL to read the end of the file.
However, an ioexception is thrown when the end of the file is read;
6. serialization of an object does not necessarily serialize the parent class Object of the object.
7. Instant attributes (temporary attributes) are not involved in the serialization process.
8. All attributes must be serializable, especially when some attributes are also objects. A serialized set requires that each element in the Set be serializable.
9. Write two objects to the file in two serialization (append ),
The size of two objects written into the file is different from that of one serialization.
Because each append operation adds a start tag and end tag to the file. Therefore, Object serialization cannot be written to a file in append mode.

Address: http://bbs.csdn.net/topics/340105750
Related Article

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.