[Java] Java IO

Source: Internet
Author: User

Java IO is a set of Java APIs for reading and writing data (inputs and outputs). The Java IO API is located in the Java.io package. In fact, the java.io package does not address all of the input and output situations, for example, the Web page is not included in the Java IO package, but is handled by the Java Enterprise version of the servlet and HTTP package.

The Java IO package focuses on the input and output of file, network stream, memory buffer (internal). The Java IO package does not contain a class to open a network socket. The network socket is open by the Java Network API, and then Java IO's Inputstream/outputstream is read from the open socket, or the data is written to the open socket.

Java NIO is used to handle non-blocking input and output operations.

Input and Output

The most common data sources or destinations are:

File

Pipeline

Network connection

Memory buffers

System.in, System.out, System.error

The essential data flow request is as follows

Source--Program--Destination

Stream

Stream is the core concept of Java IO. Stream is an endless stream of data that can be read from a stream or written to a stream. Stream has no subscript, cannot move forward backwards, and is not the same as the array.

Stream can be simply divided into byte-based (byte based), or character-based (character based).

Byte-based stream is generally called xxxxxxstream, such as InputStream, OutputStream. These streams read or write only one byte at a time, except for Datainputstream/dataoutputstream. Datainputstream/dataoutputstream is used to handle types such as Int/long/boolean.

Character-based stream is generally called xxxxxxreader or xxxxxxwriter.

The program reads data through InputStream or reader and writes data through OutputStream or Writer.

Program, Inputstream/reader, Source

Program, Outputstream/writer, Destination

If you need to write components for input or output, try to make the components dependent on inputstream/outputsteam rather than their specific subclasses, making the code more resilient. For example

New FileInputStream (filepath);
New FileOutputStream (filepath);

Reading one byte at a time from a file is very slow, and using Bufferedinputstream to read a chunk of data at a time can be much faster. Example

New Bufferedinputstream (new FileInputStream ("/users/.../a.txt"));

Buffering (buffer) also applies to OutputStream, which is used for batch writing to hard disks.

  Byte Based Character Based
Input Output Input Output Comment
Basic InputStream OutputStream

Reader

InputStreamReader

Writer

OutputStreamWriter

Arrays
Files FileInputStream FileOutputStream FileReader FileWriter
Pipes
Buffering Bufferedinputstream Bufferedoutputstream In memory, better performance
Filter
Parsing Pushbackinpustream Pushbackreader
String
Data
data-formated
Object
Utilities

InputStream and OutputStream

InputStream is the underlying class for all input stream in the Java IO API. Inputsream the following sub-class has FileInputStream, Bufferedinputstream. InputStream Example

            New FileInputStream ("/users/.../a.txt");                         int data = is.read ();              while (Data! =-1) {                System.out.print (char) data);                 = Is.read ();                }

FileInputStream is a subclass of InputStream. Exception handling is omitted from the example.

When the Read () method returns 1, the description stream and no content can be read. 1 is an int value, not a byte or short value.

The read (byte[]) method reads the byte array one time, returning the number of bytes read in. Read (byte[]) is faster than reading () read only one byte at a time. Each time you need to check the return value of Read (byte[]) to understand the size of the data being read.

The mark () and Reset () methods are mainly used to implement the interpreter, with a small case.

OutputStream is the underlying class for all output stream in the Java IO API. OutputStream the following sub-class has FileOutputStream, Bufferedoutputstrea. OutputStream Example

            New FileOutputStream ("/users/.../b.txt");             byte [] B = {' H ', ' e ', ' l ', ' l '};            Os.write (b);            Os.close ();

Write (byte[]) method writes all the bytes in a byte array to OutputStream

Flush () method, the data brush (write out) of the Outputsteam has been written to the underlying data destination. For example, if OutputStream is FileOutputStream, the bytes written out to FileOutputStream are not all written out to the hard disk. Even though the Java code has written the data out to FileOutputStream, the data may still be in the buffer. The flush () method ensures that data is brushed to the hard disk (or network).

The close () method to close the stream. Typically located in the final code block.

Reader and Writer

Reader and writer function in Java IO similar to InputStream and OutputStream, where the difference is between reader and writer-based characters.

Reader is the base class for reader in the Java IO API. The following subclasses of Reader are BufferedReader, InputStreamReader, StringReader. Example

            New FileReader ("/users/.../a.txt");             int data = reader.read ();                          while (Data! =-1) {                System.out.print (char) data);                 = Reader.read ();            }

InputStream returns a value of 8 bit (a byte) at a time (0 to 255)

Reader returns a value of (0 to 65535) at a time, possibly one or more bytes, depending on the encoding format in which the text is used.

Reader can be nested outside the InputStream to read characters.

            New FileInputStream ("/users/.../a.txt");             New InputStreamReader (IS);

Writer can be similarly nested outside the outputstream, writing characters.

            New FileOutputStream ("/users/.../b.txt");             New outputstreamwriter (OS);            Wt.write ("QQQ");            Wt.close ();

Assembly of the Reader/writer sub-class

            New BufferedReader (new FileReader ("/users/.../a.txt"));             New BufferedWriter (new FileWriter ("/users/.../c.txt"));

Resources

Java IO Tutorial, Jenkov

[Java] Java IO

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.