Java IO programming [reprint]

Source: Internet
Author: User
What is IO?

IO (Input/Output) is the computer Output/Output interface. Java. io provides a comprehensive IO Interface, including file read/write and standard device output. In Java, I/O is input and output based on a stream. All data is written to the output stream serially or read from the input stream.. In addition, Java also supports block transmission. Block IO is used in the core library java. nio. We will discuss NIO in detail later.
The advantage of stream IO is that it is easy to use and its disadvantage is that it is less efficient. Block IOHigh efficiency, but complicated programming.

Java IO Model

Java IO ModelExcellent Design, It uses the Decorator mode and divides Stream by function.You can dynamically assemble these streams to obtain the functions you need. For example, if you need a buffer file input stream, FileInputStream and BufferedInputStream should be used in combination.
   The IO System of Java is divided into two types: Input/Output and Reader/Writer. The difference is that Reader/Writer can automatically convert internal codes when reading and writing texts.Basically, all IO classes are paired, and XxxOutput corresponds to XxxInput.

Java IO tutorial

If you are familiar with the Decorator mode, you can easily see the Java IO class structure: the root interface is InputStream/OutputStream, And the IO classes acting as data sources include FileInputStream/FileOutputStream and ByteArrayInputStream/ByteArrayOutputStream, the IO classes acting as decoration functions include BufferedInputStream, BufferedOutputStream, DataInputStream, and DataOutputStream. They all inherit from the decoration interface FilterInputStream/FilterOutputStream. When I/O is used, create a data source I/O first, and then create a decoration class I/O Based on the required functions. The constructor parameter is the created data source I/O.Taking the creation of a buffer file input stream as an example, assume that the file "C:/log.txt" needs to be read from the disk ":

// Create a FileInputStream:
FileInputStream fileInput = new FileInputStream ("C: // log.txt ");
// Create a BufferedInputStream:
BufferedInputStream bufferedInput = new BufferedInputStream (fileInput );
// The obtained bufferedInput is a buffer file input stream.

Or, you can simply enter the following:

InputStream input = new BufferedInputStream (
New FileInputStream ("C: // log.txt "));
// The input obtained now is a buffer file input stream.

After you have a general understanding of Java I/O, we recommend that you take a look at the tutorial Introduction to Java I/O and I/O: Reading and Writing.

Java NIO Programming

NIO provides block I/O support. The advantage of block I/O is higher efficiency. In addition, Java NIO will directly call many advanced IO interfaces provided by the operating system, supporting block transfer and read/write locking, asynchronous IO and other functions with high efficiency. NIO programming models are channels and buffers. We recommend that you read Getting started with new I/O (Chinese ).

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.