Java input and output (i)--stream

Source: Internet
Author: User

Java input and output can be divided into two categories, streaming and non-streaming operations, streaming operations mainly concerned with the reading and writing of the contents of the file, non-streaming operations are mainly used for file management on disk. The following first learns streaming data operations.

Flow

Similar to C + +, the concept of flow is also provided in Java to handle input and output (I/O). In Java, an object from which a sequence of bytes can be read is called an input stream, and an object to which a sequence of bytes can be written is called the output stream. An input-output stream is an object that needs to bind a specific byte source and destination, which can be a standard input and output (keyboard and screen), a file or a network, or even a block of memory.

Abstract classes InputStream and OutputStream form the basis for the input-output (I/O) class hierarchy. These two classes are mainly used to handle byte input and output, that is, subclass objects that inherit the two classes are mainly used to handle the stream of bytes. In general, the need to handle characters is more common, and in order to handle characters, the reader and writer two abstract classes are available in Java to handle character input and output.

The underlying method of the InputStream class is the Read () method, which reads a byte from the input source, the return value is the read-in byte, and the return type is int. The specific type of read-in source read-in depends on the specific implementation of the abstract class, such as the FileInputStream object reads bytes from the bound file, and the System.in object reads into bytes from the standard input (keyboard). InputStream also has other methods, such as reading a sequence of bytes, skipping n bytes, and so on. The underlying method of the OutputStream class is write (int n), which is used to write a byte to the output stream. Based on these two abstract classes, there is a complete stream class family in Java to handle a wide variety of input and output problems. As shown in the following:

Similar to InputStream and Outputstream,reader and writer there are two basic methods, namely read and write, except that the output and input of these two functions are two-byte Unicode characters instead of one byte. There is also a complete family of classes to handle various character input and output problems. The class hierarchy is:

There are some common methods in the input and output classes, such as Close,flush and so on. These methods are mainly obtained through the implementation of interfaces, these interfaces mainly include closeable,flushable,readable,appendable and so on.

Combined Flow Filter

Various output stream classes in Java do a variety of functions, such as FileInputStream implementation of file flow input, DataInputStream Implementation of Java Basic data type input, they do their own functions, other stream classes can not use the functionality of other stream classes , in order to achieve a combination of functions, such as the use of the basic data type of the way to read a file, you can use:

FileInputStream fin=new FileInputStream ("File.data");

DataInputStream din=new DataInputStream (Fin);

Double s=din.readdouble ();

When it is necessary to use the direct read Java basic data type from standard input, you can use:

DataInputStream din=new DataInputStream (system.in);

Double s=din.readdouble ();

The combination of various functions can be realized by using this nested flow filter (group merging filter). It can also be used to communicate byte processing streams and character processing streams. As follows:

BufferedReader in=new BufferedReader (New InputStreamReader (New FileInputStream ("File.tax"), "UTF-8"); It's just a show.

With respect to the byte stream and the character stream, the byte stream can handle a wide variety of files, such as audio and pictures, while a byte stream can only implement text processing.

Java input and output (i)--stream

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.