Dark Horse programmer--java Learning Note 10 (IO)

Source: Internet
Author: User
Tags object serialization

1, an object from which a sequence of bytes can be read into an input stream, an object to which a sequence of bytes can be written to do the output stream. The source of these byte sequences can be a file or a network connection or a block of memory. Abstract classes InputStream and OutputStream form the basis of the I/O hierarchy. Because the byte stream is not easy to handle the information stored in Unicode, there is a writer and reader abstract class, and the classes inheriting from it are read-in and written to 2-byte Unicode characters, not individual bytes.
2, InputStream and OutputStream:
abstract int Read (): reads in a byte and reads to the end of 1, which must be overwritten in a specific class. Also read into a byte array, or skip a large number of bytes. These are all calls to it. absract void Wirte (int b): Outputs one byte, both of which are blocking calls. The int available () method allows us to check the number of bytes currently available for reading. The available method of class InputStream always returns 0, and this method should be overridden by subclasses. Close () closes the stream and frees up very limited system resources when it finishes reading and writing. When you close a stream, the buffer of the output stream is also flushed, because the characters that are temporarily placed in the buffer so that they are passed in the form of a larger package are sent out when the output stream is closed. a long skip (long n) and the input stream skips n bytes.  3, Writer and Reader:for Unicode literals, their subclasses can be used. abstract int Read () (0~65535), returns 1 at the end;abstract void Write () writes a single code element There are four additional interfaces: Closeable, flushable, readable, appendable, and the first two interfaces, respectively, using the following methods:void Close () throws IOExceptionand void Flush ()InputStream OutputStream Reader Writer has implemented the closeable (try Resource)OutputStream and writer implement the Fulshable interface,readable interface: int read (Charbuffer CB) The Charbuffer class has a sequential and random read-write access method that represents an in-memory buffer or memory image file. appendable:appendable Append (char c)The apppendable append (charsequence s) charsequence interface describes the basic properties of a sequence of char values, String charbuffer, Sringbuilder and StringBuffer both achieved it. only writer realizes it.  Four interface diagrams:

4, Combined flow filter:Fileinputsteam and Fileoutstream can provide attachment to a disk file (which can be added at the end), but can only be read into the write-up byte or byte array. Java uses a clever mechanism to separate these two responsibilities. Some streams are responsible for retrieving the byte stream from the file or from the outside, while other streams can make the bytes array into other types. So programmers have to combine the two. FileInputStream and Fileteroutputstream subclasses of these classes add more functionality to the original word stream. The byte stream is not buffered by default, and each Read,write call requests that the manipulation system distribute one bytes. LineNumberReader line number character streambufferedinputstream bufferedoutputstream byte throttle bufferDataInputStream DataOutputStream Digital StreamPushbackinputstream Stack byte stream The OutputStreamWriter class uses the selected character encoding to stream Unicode characters to a stream of bytes, while the InputStreamReader class streams the input that contains the byte (a character encoded in a character encoding).  system.in for InputStream typesystem.err System.out for PrintStream Text output:PrintWriter If the writer needs automatic flushing mode, all the characters of the buffer will be sent to their destination as long as println is called. Unlike the PrintStream class, if automatic refresh is enabled, it is possible to do this only if one of the methods of println, printf, or format is called, rather than when the newline character is printed exactly.  These methods use the platform-owned line delimiter concept, rather than line breaks. printstream for automatic refresh, you can create a printstream, which means that the flush method can be called automatically after the byte array is written, either by calling one of the println methods, or by writing a newline character or byte (' \ n '). all characters printed by PrintStream are converted to bytes using the platform's default character encoding. The PrintWriter class should be used in cases where characters need to be written rather than bytes written. text input:Scaner BufferedReader text A full line of input 5, Character set: The CharSet class unifies the conversion of the character set. set<string> alias (), returns the collection of aliases for the character setforname (String name), which returns a character objectAvailablecharsets (), whether the character set is availableencoded string:stirng str = ...;Bytebuffer buffer = Cset.encode (str);byte[] bytes = Buffer.array ();decode string:byte[] bytes = ...;Bytebuffer bbuf = bytebuffer.wrap (bytes, offset, length);Charbuffer cbuf = Cset.decode (bbuf);String str = cbuf.tostring (); 6, read and write binary data: DataOutput and Datainput Interface: interface defines the method:Write (read) CharswritebytesWirteintWriteshortWritelongwritedoubleWritecharWritebooleanwriteUTFSkip : 7, Random Access file: The Randomaccessfile class can find or write data anywhere in the file. Disk files are randomly written, but data from the network is not. You can open a file by using the string "R" "RW" "w". It implements the Datainput DataOutput interface. void Seek (long pos)long Getfilepointer () Zipinputstream Zipoutputstream Compressed document class: 8, Object flow and serialization: Writes an object to the stream, and then reads it back. wrteiobject (Object obj)Object Read ()All classes that you want to store or restore in the object stream should be modified, and these classes must implement the Serializable interface without any method. Each object is saved with a sequence number, which is why this mechanism is called object serialization, and the algorithm is as follows. When writing objects:Associate a serial number for each object reference you encounterfor each object, when first encountered, save its object data into the streamif an object has been previously saved, write only "same as the object with the serial number x previously saved". when you read it back, the whole process is reversed:for an object in a stream, build it the first time it encounters its serial number, initialize it with the data in the stream, and then record the association between the sequence number and the new object. gets the object reference associated with the sequence number when it encounters the same as the object with the previously saved sequence number x.  Some data cannot be serialized, so they are marked as transient, and static is not serializable. a serializable class can add validation or other behavior to the default read-write behavior. private void ReadObject (Ojbectinputstream in) throws IOException, ClassNotFoundException private void WriteObject (Ojbectoutstream out) throws IOException, ClassNotFoundExceptionIn addition to having the serialization mechanism for saving and recovering data, classes can implement their own mechanisms, but this class must implement the Externalizable interface: publc void Readexternal (Ojbectinputstream in) throws IOException, ClassNotFoundException Public void Writeexteranl (Ojbectoutstream out) throws IOException, ClassNotFoundExceptionto remember to add the Readresolve method to all type-safe enumerations in the legacy code and to all classes that support a single-column design pattern, ReadObject calls it by default and uses its return value as its return value to ensure uniqueness.  

Dark Horse programmer--java Learning Note 10 (IO)

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.