Read and Write Data Using Java output/output streams

Source: Internet
Author: User

 

The Java input/output (I/O) mechanism provides a simple and standardized API for reading and writing characters and bytes of data from different data sources. Data Stream

All Java I/O mechanisms are based on data streams, which represent the flow sequence of character or byte data. Java I/O Stream provides a standard method for reading and writing data. Any object in Java that represents a data source can read and write data in the form of data streams.

Java. io is the main software package for most data stream-oriented input/output classes. This package contains two abstract classes: InputStream and OutputStream. The input/output classes of all other surface image data streams must be extended.

The java. io package provides classes and interfaces that define useful abstractions at the top of the read/write operations provided by the InputStream and OuputStream classes. For example, the ObjectInputStream class allows you to read data from input/output streams as objects, while the ObjectOutputStream class allows you to write Java objects into data streams.

Optimize the read/write process
JDK 1.1 adds a set of read/write classes that provide more useful abstraction and better input/output performance than existing data stream classes. For example, BufferedReader and BufferedWriter are used to read and write text from character-based input and output streams. The BufferdReader class caches characters to read strings, arrays, and text strings more efficiently. BufferedWriter class cache characters to write strings, arrays, and text strings more efficiently. BufferedReader and BufferedWriter can be set as needed.

The reader and writer classes in the Java input/output architecture include LineNumberReader, CharArrayReader, FileReader, FilterReader, PushbackReader, PipedReader, StringReader, and other classes. These classes are the wrap classes at the top of the InputStream and OuputStream classes, so they provide methods similar to the InputStream and OuputStream classes. However, these classes provide more efficient and useful abstractions for reading and writing specific objects, such as files, character arrays, and strings.

Read data
When you extract an input stream from a corresponding data source object or create a reader object, an input stream is automatically opened. For example, to open an input stream for a file, we only need to pass the file name to the constructor of the Java. io. FileReader object in the following way:

Java. io. FileReader fileReader = new java. io. FileReader ("/home/me/myfile.txt ");

To read one byte data in the input stream at the bottom layer of FileReader in sequence, you only need to use the read method without parameters. The code segment in Table A reads text data from A file, one character at A time, and then writes it into System. out.

To read the specified number of bytes from the input stream to the char array, you only need to use the read method with a char [] parameter. The length of the array is used to determine the number of characters to be read. Table B demonstrates this technology.

To close an input stream and all system resources used by the stream, you only need to call the close method in the following ways:
FileReader. close ();

Write Data
Like an input stream, the output flow is automatically opened when you extract it from the corresponding data source or create a write object. For example, to open an output stream for a file, we pass the file name to the constructor of the java. io. FileWriter object, as shown below:

Java. io. FileWriter fileWriter = new
Java. io. FileWriter ("/home/me/out.txt ");

To write a specific character to the output stream, you can use the write method with an int parameter. The int parameter indicates the character to be entered.

Int aChar = (int) X;
FileWriter. write (aChar );

To write a specific number of characters in the char array at the given offset address of the output stream, you can use the write method with a char [] parameter, an int offset parameter, and an int length parameter, the following is an example:

FileWriter. write (buffer, 0, byteCount );

To close an output stream and release all related system resources, you can use the close method as follows:

FileWriter. close ();

To forcibly write all the data in an output stream, use the following flush method:

FileWriter. flush ();

Combine them all
We can use the functions we have learned to read data from one file and write it to another file at the same time, as shown in Table C.

Summary
Java's input/output mechanism adds a simple and standardized API for reading and writing characters from different data sources. Your experience in using a Java stream for a data source makes it easy for you to use other data source types provided by Java.

In our next article, we will begin to learn about the network and remote communication architecture of the Java platform. We will extend our discussion of Java streams to these environments and demonstrate how to open a remote data source, writing data and reading data like operating a local data source, such as a file.

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.