Java Summary-Files & streams

Source: Internet
Author: User
Tags object serialization

Recently learned Java input and output, the brain has two points of confusion, but more than before the idea seems clear a lot. The brain just received this information, the whole is to be confused, but also the file is the flow, the file is exactly what to do, what is the flow? Well, later, think about, in fact, it is not difficult to understand. The input and output in Java is like the brain receiving information. The file is like the brain, where the information is stored, the stream is something like Sonic, the ear receives it, but not the brain (you're right, but I just don't listen.) )

1. What is file, what is Randomaccessfile, and when is it used?

1) First of all to illustrate the role of the file class, the file class is the only object in the IO package to represent disk file information. The role of the file class is to create files, delete files, rename files, and interpret whether a file has Read permissions, and whether it exists. File includes a directory.

2) Randomaccessfile: Used to read and write file data, which is based on the pointer to read and write files. Commonly used for operations such as copying file data. The copying of the files can improve the efficiency of data reading by increasing the amount of data read each time to reduce the number of data read.

2. What does the flow mean, and what is the difference?

1) Classification of streams.

A) The direction of the flow is divided into: input stream and output stream. Input refers to the data flow program, the output refers to the data from the program outflow.

b) The Division of labor from the flow is divided into: node flow and processing flow.

c) The content from the stream is divided into: byte stream oriented and character stream oriented.

2) Several commonly used streams:

A) file stream: FileInputStream, FileOutputStream, low-level stream (node stream), which is used to read and write bytes from the specified file, respectively.

b) Buffer stream: Bufferedinputstream, Bufferdoutputstream, advanced streaming, can improve read efficiency.

C) Object flow: ObjectInputStream, ObjectOutputStream, advanced flow, respectively, for object deserialization and object serialization.

Corresponding method: ReadObject () and writeobject (Object O) method.

Note : If you want the object to be written out, the premise is that the class to which the object belongs must implement the Serializable interface.

d) Conversion flow: InputStreamReader, OutputStreamWriter. The ISR and OSW are equivalent to the link between the byte stream and the character stream, similar to the effect of the converter.

e) buffer character stream: BufferedReader, PrintWriter (internally using BufferedWriter implementation).

Note: Although the character stream is in characters, the underlying is actually read and written in bytes, so the character stream is inherently capable of converting bytes to characters or characters into bytes. So all character streams are high-level streams. It is convenient for us to read and write character data. No more worrying about the conversion of characters to bytes.

The low-level flow, which is assembled and processed at the outer layers, forms a more efficient flow with more complete functions. For example, the file stream can improve the reading and writing efficiency after the buffer flow in the outer packing; The file stream can be used to read and write files to the object stream.

Processing flow generally need to add to the node stream to play a role, but some streams such as printwriter can be used alone, but read and write data is fixed, rather than the flow of their own assembly flexibility.

Java Summary-Files & streams

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.