Java Io Principles and Applications

Source: Internet
Author: User

Overview

I/O is an unavoidable problem in programming.ProgramPerformance bottleneck of running (NIO was introduced after Java 1.4 to improve I/O performance). Java has a wide range of Io stream categories, and the IO package contains a large number of classes and interfaces. In terms of classification, the main categories are as follows:

    1. Byte-based I/O interfaces: inputstream and outputstream
    2. Character-based I/O interfaces: writer and Reader
    3. Disk-based I/O interfaces: File
    4. Network Operation-based I/O interface: Socket

Before looking at the specific interface, we will first introduce a design mode:Decorator patternDecorator mode, because the core of Java Io isDecorator is used.

When using Java Io, you will often see the following statement:

Inputstream in = new bufferedinputstream (New fileinputstream ("FILENAME "));

The relationships between these classes are as follows:

Public classBufferedinputstreamExtends filterinputstream
Public classFilterinputstreamExtends inputstream
Public classFileinputstreamExtends inputstream

I wrote a decorative relationship model by imitating the relationships of these classes:

/**  * Programmers * writeCodeThe abstract decorator of the function. * Corresponding to inputstream  */  Interface  Coder {  Void  Writecode ();}  /**  * Java programmer * has a specific decorator for writing Java code * Corresponds to fileinputstream  */  Class Javacoder Implements  Coder {@ override  Public  Void  Writecode () {system. Out. println ( "Write Java code! " );}}  /**  * Programmers with dreams (not directly related to Java programmers) * have the abstract meaning of the decorator * corresponding to filterinputstream  */  Class Havadreamcoder Implements  Coder {coder;  Protected  Havadreamcoder (CODER ){  This . Coder = Coder;} @ override Public   Void  Writecode () {coder. writecode ();}}  /**  * Advanced Java programmers * decorator with specific functions (Design Code) * corresponding to bufferedinputstream  */  Class Seniorjavacoder Extends  Havadreamcoder {seniorjavacoder (CODER ){  Super  (CODER) ;}@ override  Public   Void Writecode () {design (); coder. writecode ();}  Public   Void  Design () {system. Out. println ( "Design Code! " );}  //  Additional methods-Senior programmers will read excellent source code      Public   Void  Readcode () {system. Out. println ( "Read nice code! " );}} 

The usage is as follows:

Seniorjavacoder coder = new seniorjavacoder (New javacoder ());
Coder. writecode ();

Print result:

Design Code!
Write Java code!

Senior Java programmers can solve most of the problems, but one day we find that a very complicated problem cannot be solved by senior Java programmers, however, we cannot afford to dismiss a senior Java programmer (because she is a plmm), so we need to recruit a Java programmer (suchDatainputstream), This Java programmer is more advanced than the advanced Java programmer in writing code, and he will write C/C ++ and other code.

Byte-based I/O operation interface

Level of inputstream related classes


Outputstream class hierarchy

Character-based I/O operation interface

Reader-related class hierarchy

Writer-related class hierarchy

Data persistence or network transmission are performed in bytes. Therefore, conversion from character to byte or byte to character is required. The conversion process of characters to bytes is as follows:

 

Byte and character conversion APIs

Read:

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.