IO stream in Java

Source: Internet
Author: User

Stream : Java streams are Some of the classes that Sun has developed specifically for file manipulation, data transfer, But a name called a stream . all open streams must be closed , because the open flow is very resource-intensive , and it's not safe . .

The object in the stream is for the current operation of the program , the input refers to the import of data into the program , the output refers to the current program's data exported to the external

All streams implement the closeable ( meaning that programmers need to manually close the program ) interface for closing the stream

All output streams also implement flushable when inheriting closeable , which forces the cached data to be brushed into the file

a typical stream

Raw Stream :( Open connection , read Data )

FileInputStream

FileOutputStream

FileReader

FileWriter

Packaging Flow :( enhanced on a primitive basis )

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

BufferedReader

BufferedWriter

Bufferedinputstream

Bufferedoutputstream

Convert stream : converts byte streams to character stream

InputStreamReader

OutputStreamWriter

Format the output stream : to compose the data before it is output

PrintWriter

PrintStream: standard output stream , which outputs data to the console by default

data storage between different platforms ( operating systems ) , read in different ways , in order to compensate for these differences , form a unified , There are two types of flow

DataOutputStream

DataInputStream

Serialization : An object is a way of encapsulating data and does not encapsulate logic . It is not a problem to use objects in the current memory category , But if you go beyond the scope of the current memory scope, you need a logical , other places don't necessarily have the logic of the current object , but the data in it needs to be read in a certain way. , so we're going to create the logical code for the object. , This is called serialization of objects.

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

ObjectOutputStream serializing A Java object to a hard disk or remote computer

ObjectInputStream deserializing data from a hard disk into JVM Memory

Serialization is to save the data in the object to a file by two classes,not to copy the entire class into a file,Instead, serialize the data in the class-instantiated object into a file,the original class structure file must also be saved.to ensure that the class structure files before and after serialization are consistent,all realized.Serializablethe class of the interface and its subclasses will have an implied member propertySerialversionuid:This member variable is used to saveJVMa unique encoding for the build,of course,You can also explicitly specify the

After serialization,to deserialize,the original class file must be called,if the original class file does not exist,deserialization will be an error .,this time,we need to regenerate the class file,but,if the contents of the class are changed,rebuild will also fail,realized theSerializableinterface of the class file.Serialversionuidit's all different.,This has also been serialized into the object file,It's better to deserialize than this code.,inconsistencies will be an error ., Serialversionuidencoding is content-bound,If the data is changed,and the numbers change..but we can explicitly specify it ourselves.Serialversionuidto circumvent the problem .(static final long serialversionuid = 1L),we specify later in the class original encoding file, the JVMWill not be specified again,so the original encoded file,class file,the encoding in the serialization file is always the same,Even if we delete the original class file,Regenerate a;We even changed the contents of the class file .,can also be deserialized successfully

Common methods

All streams have a close () method that closes the stream after the stream is used

in inputstream and Reader

Read (): reads data from the input stream and has different reads depending on the parameters

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

in outputstream and Writer

Write (): reads the specified data into this output stream

Flush (): forces all buffered content to be brushed into this output stream

IO stream in Java

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.