IO stream in java and javaIO stream

Source: Internet
Author: User

IO stream in java and javaIO stream

Stream: java stream is a class developed by Sun for file operations and data transmission, but it is called a stream. all opened streams must be closed because the opened streams consume resources and are insecure.

The objects in the stream are for the current operation program. The input refers to the import of data into the program, and the output refers to the export of data from the current program to the external

All streams implement the Closeable (meaning the program that requires the programmer to manually close) interface, used to close the stream

All output streams inherit Closeable while implementing Flushable, which is used to forcibly fl cached data into files.

 

16 common streams

Original stream: (open connection and read data)

FileInputStream

FileOutputStream

FileReader

FileWriter

Packaging stream: (enhanced on the basis of the original)

Cache stream, in order to speed up, read multiple times, write once

BufferedReader

BufferedWriter

BufferedInputStream

BufferedOutputStream

Conversion stream: Convert byte streams to byte streams.

InputStreamReader

OutputStreamWriter

Format the output stream: You need to typeset the data before it is output.

PrintWriter

PrintStream: standard output stream. Data is output to the console by default.

Data is stored on different platforms (operating systems) and read in different ways. To make up for these differences and form a unified system, the following two streams are available:

DataOutputStream

DataInputStream

Serialization: an object is a data encapsulation method without encapsulation logic. there is no problem with the use of objects in the current memory scope, but logic is required if the scope of the current memory is exceeded, and the logic of the current object is not necessarily found elsewhere, but the data in it needs to be read in a specific way, so we will create the logic code corresponding to the object together, which is called object serialization.

This class is required to implement the java. io. Serializable Interface

ObjectOutputStream serializes a java object to a hard disk or remote computer

ObjectInputStream deserializes data in the hard disk to the jvm memory.

Serialization stores data in an object to a file through two classes. instead of copying the entire class to a file, serialization serializes the data in the object after class Instantiation to the file, the original class structure file must also be saved. to ensure that the class structure files before and after serialization are consistent, all classes that implement the Serializable interface and their subclasses will have an implicit member attribute serialVersionUID: this member variable is used to save a unique encoding generated by jvm. Of course, you can also explicitly specify

After deserialization, the original class file must be called. If the original class file does not exist, the deserialization will report an error. At this time, we need to re-generate the class file. However, if the class content is changed, re-generation will also fail. The serialVersionUID of the class file implementing the Serializable interface is different and has already been serialized into the object file, this encoding will be compared during deserialization. If the encoding is inconsistent, an error will be reported. The serialVersionUID encoding is bound to the content. If the data is changed, the number will change. however, we can explicitly specify the serialVersionUID to avoid this problem (static final long serialVersionUID = 1L). After we specify it in the Class Original encoding file, the jvm will not specify it again, in this way, the encoding in the original encoding file, class file, and serialization file will always be the same, even if we delete the original class file and regenerate one, or even change the content of the class file, deserialization is successful.

 

Common Methods

The close () method is available in all streams, and the stream is closed after the stream is used.

InputStream and Reader

Read (): read data from the input stream. Different Reading methods are available based on different parameters.

Skip (): skips and discards some data in the input stream.

In OutputStream and Writer

Write (): Read the specified data into the output stream.

Flush (): force all buffered content into this output stream.

 

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.