I/O Stream

I/O Streams

all streams present the same simple model to programs that use them:A stream is a sequence of data.A programs uses an input stream to read data from a source , one item at a time.


I/O Stream_第1张图片
Reading information into a program

A program uses an output stream to write data to a destination,one item at a time.


I/O Stream_第2张图片
writing information from a program

a source or destination can also be another program, a peripheral device, a network socket ,or an array.

we will use the example file xanadu.txt:

In Xanadu did Kubla Khan

A stately pleasure-dome decree:

Where Alph, the sacred river, ran

Through caverns measureless to man

Down to a sunless sea.


1. Byte Streams:

Programs use byte streams to perform input and output of 8-bit bytes.

All byte stream classes are descended from InputStream and OutputStream .

Explore FileInputStream and FileOutputStream: (one byte at a time)


I/O Stream_第3张图片


I/O Stream_第4张图片
Simple byte stream input and output

Always Close Streams:

CopyBytes uses finally block to guarantee that the both streams will be closed even if an error occurs.

one possible error is that CopyBytes was unable to open one or both files.

When not to Use Byte Streams:

it actually represents a kind of low-level I/O .we should avoid it.

Byte streams should only be used for the most primitive I/O.

all other stream types are built on byte streams.


2. Character Streams:

Java platform stores character values using Unicode conventions.

Character stream I/O automatically translates this internal format to and from the local character set.

All character stream classes are descended from Reader and Writer.

Explore FileReader and FileWriter:



I/O Stream_第5张图片

Notice that both CopyBytes and CopyCharacters use an int variable to read to and write from . however, in CopyCharacters , the int variable holds a character value in its last 16 bits; in CopyBytes, the int variable holds a byte value in its last 8 bits.

Character Streams that use Byte Streams:

Characters streams are often  "wrappers" for byte streams.

The character stream uses the byte stream to perform  the physical I/O, while the character stream handles transition between characters and bytes.

There are two byte-to-character "bridge" streams: InputStreamReader an OutputStreamWriter. use them to create character streams.

Line-Oriented I/O:

Character I/O usually occurs in bigger units than single characers.

One Common unit is the line: it ends with a line terminator. a line terminator can be a "\r\n", a "\r", or "\n".

Explore BufferedReader and PrintWriter:


I/O Stream_第6张图片


3. Buffered Streams:

Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS.This can make a program much less efficient, since each such request often triggers disk access, network activity ,or some other operations that is relatively expensive.

Buffered input Streams read data from a memory area known as a buffer.

Buffered output Streams write data to a buffer.

A ·program can convert an unbuffered stream into a buffered stream using the wrapper idiom.

the unbuffered stream object is passed to the constructor for a buffered stream object  class.

There are four buffered stream classes used to wrap unbuffered streams:   BufferedInputStream and BufferedOutputStream create buffered byte streams, while BufferedReader and BufferedWriter create buffered character streams.


4. Scanning and Formatting:

Scanning

objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.

1.Breaking Input into Tokens:

By default,a scanner uses white space to separate tokens


I/O Stream_第7张图片

To use a different token separator, invoke useDelimiter(), specifying a regular expression.

2.Translating Individual Tokens

你可能感兴趣的:(I/O Stream)