Standard I/O Stream and file 2 in Java (j2se entry 17)

Source: Internet
Author: User

Random File Access
RondomaccessfileClass allows random access to files. This class also supports direct output of various data types.
Getfilepoint() To know the pointer position in the file, useSeek() Positioning.
Mode ("R": random read; "W": Random write; "RW": random read/write)
1) two interfaces are implemented:DatainputAndDataoutput;
2) read and write as long as the file can be opened;
3) The file pointer can be used to read and write the specified position of the file;
4) access all read () and write () operations in datainputstream and dataoutputstream;
5) How to move data in the file:
A. LongGetfilepointer():Returns the current position of the file pointer..
B. VoidSeek(Long POS ):Set the file pointer to the given absolute position.
C. LongLength():Length of the returned File.

Ghost stream

Character stream can solve the character encoding problem in programming. From character to integer, a one-to-one correspondence relationship is established between the character set and the Integer Set. Even if it is called encoding, ing from integer to character is called decoding.

Encoding Problems:
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.

Encoding method:
Each character corresponds to an integer. Different countries have different codes. garbled characters are generated when the encoding and decoding methods are inconsistent. Because of the earliest development of software in the United States, each type of encoding is compatible with ASCII, so there is no garbled english.
ASCII (numbers, English) 1 character occupies one byte (all the character sets are compatible with ASCII)
ISO8859-1 (Europe) 1 character occupies a byte
GB-2312/GBK 1 character occupies two bytes
Unicode 1 character occupies two bytes (network transmission speed is slow)
The UTF-8 is variable-length bytes. For an English byte, for a Chinese character, two or three bytes.


Inputstreamreader and outputstreamwriter (bridge converter for converting byte streams)
These two classes are not used for direct input and output. They convert byte streams into the bridge converter of the dense stream and can specify the encoding/decoding method.

Reader and writer (parent stream class, all parent stream types)

1) Use of Java TechnologyUnicodeAnd provides a 16-bit stream to process characters in a similar way.
2)Inputstreamreader and outputstreamwriter are used as interfaces between byte streams and bytes streams. 
3) if a reader and writer connected to the stream are constructed, the conversion rules will switch between the byte encoding and Unicode defined by the default platform.

Bufferedreader/(bufferedwriter, not commonly used) (these two classes require bridge conversion)
Printwriter (character output stream with cache, no bridge conversion required)

Common Input/output types, which do not require bridging. For other methods, see the API documentation.
Both of the above are filter streams, and other node streams must be used as parameters to construct objects.
Bufferedreader method: Readline (): String. If its return value is null, the read is complete. Pay attention to writing line breaks when writing again. Otherwise, blocking may occur.
Bufferedwriter method: newline (). This method will write a line break.
Printwriter method: println (.... String, object, etc.), write (), println (...), this method does not have to write line breaks, will automatically wrap when used.

Note: InWhen a stream with a buffer is used, the flush () method is required after the input to send the buffer data..

Principle: ensure the consistency of codec methods to avoid errors.
Java. Io packageInputstreamreaderBridge conversion class for the input stream from byte to the upstream stream. This class can be used to set the character conversion mode.
Outputstreamwriter: The byte stream Bridge of the output stream is converted to the bytes stream.
BufferedreaderYesReadline() Makes character input more convenient.

In an I/O Stream, all input methods are blocking methods.
Bufferwrite adds a buffer to the output character because it has few methods, so it uses the parent class printwrite. It can use byte stream objects, and there are many methods.

Objectinputstream and objectoutputstream (Object Stream)

An object stream is a filter stream that requires a node stream as a parameter to construct an object. It is used to directly write an object into a file and read an object from the file. Only objects of the serializable interface type can be read and written,The serializable interface is a flag interface, and no method is defined.. The object is serialized into a binary code.

Writeobject (o) and readobject () are the methods used for object read/write operations.

Object o = new Object();
FileOutputStream fos=new FileOutputStream("Object.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(o);
oos.close();

FileInputStream fis =new FileInputStream(“Object.txt”);
ObjectInputStream ois =new ObjectInputStream(fis);
Object o = (Object)Ois.readObject();
ois.close();


An eofexception exception object is returned after the object stream is read.

If there are other types of objects in a class, this class implements the serializable interface. During Object serialization, attributes in this class are also required to be able to serialize objects (except for basic types ).

Note: For object stream operations, you must write the object once. If you use the append mode to write the object, only the object written last time will be read. When you use the object stream to write the object, first, a header is written, then data is written, and the end symbol is added. If you use the append method to write data, the end symbol is written downward, however, only the ending character is read, and data written again in the future will be lost.

Note: When writing an object using an object stream, you must write the object to the file once. You cannot append the object to the file.

The serialver command determines whether an attribute or object can be serialized,
Serialver testobject (testobject must be compiled, that is,. Class)
Execution result: if it is not serializable, a message is displayed, indicating that it is not serializable. If serialization is possible, the serialized ID: uid will appear.

ExternalizableThis isSerializableIt allows you to customize how to serialize objects.
Readexternal (objectinput in), writeexternal (objectoutput out) are two methods in this interface. You can use these two methods to customize the serialization process. This method is not safe. You can call the above two methods to change the object state.

TransientIt can only be used to modify attributes. This attribute will be ignored during Object serialization.
Transient int num;
Indicates that this attribute is ignored when the attribute is serialized (that is, it is ignored to make it persistent ). All attributes must be serializable, especially when some attributes are also objects.

Java. util. stringtokenizer class, which is used for string truncation.
Stringtokenizer (parameter 1, parameter 2) separates files by a certain symbol
Stringtokenizer (S, ":") is separated by ":", and s is an object.
Supplement:-1 is returned when the byte stream ends, null is returned when the byte stream ends, and eofexception is returned when the object stream ends.
Extended -------> exceptions are often used in Process Control, and exceptions are also a form of return for methods.

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.