Java language guided note Chapter 9 IO

Source: Internet
Author: User

java.io9.1.1 character Stream

Reader provides APIs and partial implementations for Reader (reader), which is a stream that reads 16-bit characters;

Writer provides APIs and partial implementations for the writer, which is a stream that writes 16-bit characters.

Generally used to read and write text information, because you can handle any character in the Unicode character set, and the byte stream can only handle iso-latin-18 bytes??

9.1.2 byte stream

Read and write 8-bit bytes, using InputStream and OutputStream descendants.

InputStream and OutputStream provide some API and partial implementations for the input stream, which reads a 8-byte stream, and the output stream, which writes 8-bit bytes. Typically used to read and write binary data, images and sounds.

9.1.3 Understanding I/O Super class

All streams are opened automatically when they are created, and the stream should be closed by the Close method of the call flow, or the garbage collector will automatically close it.

9.2 Using Streams

I/O type: Memory, pipe, file, junction, object serialization, data conversion, counting, previewing, printing, buffering, filtering, converting between bytes and characters.

9.2.1 How to use file streams

File streams FileReader, FileWriter, FileInputStream, and FileOutputStream both read and write to a file on the native file system. You can create a file stream from a file name, in the form of a string, a file object, or a FileDescriptor object.

Import java.io.*;

public class copy{

public static void Main (string[] args) throws ioexception{

File Inputfile = new file ("Farrago.txt");

File OutputFile = new file ("Outagain.txt");

FileReader in = new FileReader (inputfile);

FileWriter out = new FileWriter (outputFile);

int C;

while ((c = In.read ())! =-1) {

Out.write (c);

}

In.close ();

Out.close ();

}

}

FileReader and FileWriter Read and write 16-bit characters. When reading and writing, these streams encode characters according to the default character encoding. The name of the default character encoding can be obtained by calling System.getproperty ("file.encoding"). To specify a character encoding other than the default character encoding, you should construct a outputstreamwriter on a fileoutputstream and specify the encoding.

9.2.2 How to use pipeline flow

Pipelines are used to link the output of one thread to the input of another thread.

The program Rhymingwords uses Pipedreader and pipedwriter to connect the input and output of its reverse and sort methods to create a "rhyming" list of words over and over.

procedure See Rhymingwords.java.

FileReader words = new FileReader ("Words.txt");

Reader rhymedwords = reverse (sort (reverse (words));

One of the reverse methods:

public static reader reverse (reader source) throws IOException {

BufferedReader in = new BufferedReader (source);

pipedwriter pipeout = new PipedWriter ();

Pipedreader Pipein = new Pipedreader (pipeout);

PrintWriter out = new PrintWriter (pipeout);

New Reversethread (out, in). Start ();

return Pipein;

}

Create both ends of the pipeline (PipedWriter and Pipedreader), and connect them by constructing Pipedreader on the PipedWriter. The content written to PipedWriter is read from Pipedreader. This connection is made up of pipelines.

The reverse method initiates a reservethread thread that writes its output to PipedWriter and returns Pipedreader to the caller. The caller then schedules a thread to read the data from the Pipedreader.

9.2.3 how to encapsulate a stream

BufferedReader in = new BufferedReader (source);

......

PrintWriter out = new PrintWriter (pipeout);

This code opens a bufferedreader,source on source that is another different type of reader. This essentially encapsulates the source into the BufferedReader. The program reads from BufferedReader, BufferedReader and then reads from source. This makes it possible to use BufferedReader's convenient ReadLine method.

9.2.4 How to connect files

Sequenceinputstream Create a single input stream from multiple input sources?

Import java.io.*;

public class concatenate{

public static void Main (string[] args) throws ioexception{

Listoffiles mylist = new Listoffiles (args);

Sequenceinputstream s = new Sequenceinputstream (mylist);

int C;

while ((c = S.read ())! =-1) {

System.out.write (c);

}

S.close ();

}

}

Concatenate first creates a Listoffiles object named MyList, which is initialized by the command-line arguments entered by the user. The MyList object is an enumeration that sequenceinputstream using this MyList object to get a new inputsream when needed.

9.2.5 Operation Filter Flow

The filter stream filters data as it reads and writes data from the stream. Filter flows include FileReader, Filewrriter, FileInputStream, and FileOutputStream.

1. Using the filter flow

Attaches the filter stream to another input or output stream when it is created. For example: Attaching a datainputstream to a standard input stream:

DataInputStream in = new DataInputStream (system.in);

String input;

while (input = In.readline ()) = null) {

...

}

This can be done using more convenient reading methods implemented by DataInputStream, such as ReadLine.

2. How to use DataInputStream and DataOutputStream

3. How to write your own filter flow

Java language guided note Chapter 9 IO

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.