Java know how much (66) Overview of input and output (IO) and streams

Source: Internet
Author: User
Tags java throws

Input and output (I/O) refers to the operation of a program interacting with an external device or other computer. Almost all programs have input and output operations, such as reading data from a keyboard, reading data from a file on a local or network, or writing data. The input and output operations can be used to receive information from the outside world, or to transmit information to the outside world. Java implements these input and output operations with streams, which are represented by a unified interface, making program design easier.

The concept of Java streams

Stream is the flow of data between parts in an input/output operation of a computer. The stream can be divided into input stream and output stream according to the direction of data transmission. The data in a stream sequence in the Java language can be either unprocessed raw binary data or a data that conforms to a particular format after certain encoding processing.

1. Input/output stream
In Java, different types of input and output sources are abstracted as streams, where the input and output data is called the data stream. A data stream is a channel in which a Java program sends and receives data that includes an input stream and an output stream. Typically, an input stream is used to read data in an application, and the output stream writes data. The characteristic of streaming input and output is that data acquisition and transmission are carried out in the sequence of data sequence. In contrast to the program, the output stream is written to the storage medium or data channel, and the input stream reads data from the storage medium or data channel, in general there are the following points about the characteristics of the stream:

    • FIFO, the first data written to the output stream is first read by the input stream.
    • Sequential access, you can write a sequence of bytes to the stream one after the other, read a sequence of bytes in the write order, and cannot randomly access the intermediate data.
    • Read-only or write-only, each stream can only be one of the input stream or output stream, can not have two functions at the same time, in a data transmission channel, if you want to write data, but also read the data, you have to provide two streams respectively.


2. Buffered streams
In order to improve the efficiency of data transmission, the concept of buffer stream (Buffered stream) is introduced, that is, a buffer is provided for a stream, and a buffer is a piece of memory dedicated to transmitting data.

When writing data to a buffered stream, the system sends the data to the buffer instead of sending it directly to the external device. The buffer automatically records data, and when the buffer is full, the system sends all the data to the appropriate external device. When the data is read from a buffer stream, the system actually reads the data from the buffer, and when the buffer is empty, the system automatically reads the data from the relevant external device and reads as much data as possible to fill the buffer. The purpose of using data flow to process the input and output is to make the input and output operation of the program independent of the related equipment, because the program does not need to pay attention to the details of the implementation of the specific equipment (details are handled by the system), so for various input, as long as the flow to do the processing, do not need to modify the source program

Overview of I/O stream classes

To facilitate streaming, the Java language provides a java.io package in which each class represents a specific input or output stream. In order to use these flow classes, you need to introduce this package when programming. Java provides two types of input and output streams: a byte-oriented stream, data processing in bytes as the basic unit, and a character-oriented stream for processing character data. A byte stream (byte stream) reads and writes a 8-bit binary number, also known as a binary byte stream or a stream of bits. The character stream reads and writes a 16-bit binary number at a time, and it is processed by a character rather than bits. It is important to note that in order to satisfy the internationalization of the characters, the Java language character encoding uses 16-bit Unicode code, and the ordinary text file is 8-bit Ascⅱ code.

The class in java.io is shown in hierarchy 10-1.


Figure 10-1 Top level hierarchy diagram for Java.io package


For some frequent device interactions, the Java language system has booked 3 stream objects that can be used directly, namely:

    • System.in (standard input), which typically represents keyboard input.
    • System.out (standard output): usually written to the monitor.
    • System.err (standard error output): Usually written to the monitor.

The steps to use a byte stream and a character stream in the Java language are basically the same, taking the input stream as an example, first creating a stream object that is related to the data source, and then using the Flow object's method to enter data from the stream, and finally the close () method to close the stream.

Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminates

Java know how much (66) Overview of input and output (IO) and streams

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.