IO Java Fundamentals -

A What is the IO?
The IO: (the Input / the Output): input and output.
The IO device: computers and communications equipment.
Input device: a microphone, a scanner, a keyboard, a mouse, etc. The
output devices: monitors, printers, projectors, headphones, audio.

Second, why the program needs IO it?
IO operation is a relative process, in general, we have a program of thinking about (memory programs).
Program needs the data: the incoming data into the program, enter.
That need to store data, the data transfer to others: output.

Three .IO Illustration:
Here Insert Picture Description
Here Insert Picture Description

And operation template classification .IO four streams:
Classification IO stream, different viewpoints, the classification is not the same drop:
1): The flow is divided: the input and output streams.
2): The data division unit: byte stream and a stream of characters.
3): the division of functionality: packaging nodes and stream flow.

Four base flow :( output stream of bytes, the byte input stream, the output stream of characters, the character input stream )
of the four streams are abstract base class: the other streams are inherited from the four base flow
that we can not create four Object large base flow, which can only create a subclass of objects.
Whatever the stream, there are close method is used to close the resource.
If the operation file, you have to open a stream object we have associated disk file, if you do not close the resource, then the disk's file has been referenced by the program can not be deleted, it can not be changed.
Here Insert Picture Description
IO family system:
Here Insert Picture Description
Here Insert Picture Description
stencil operations IO streams:
1): create the source or target objects (dug wells).
take the file stream example:
enter the following: the the flow of data into the program file, the file is the source, the target program.
output operation: the flow of data in the program to a file, the file is the target, the program source.
2): Create IO stream object ( water).
input: Create an input stream objects.
output: Create an output stream object.
3): a specific IO operation
input operations: an input stream object read method.
output operation:. Write method of the output stream object
4): Close the resource (do not forget). Once the resource is closed, you can not use The objects, otherwise an error.
Input operations: an input stream object .close ();
output operation: Output Stream object .close ().

Liuzizhenyan operation IO streams:
read in, write out .
Time coming: Come emphasized is input, read instructions are read method.
Write out: stressed out is the output, write write method is explained.

V. presentation stream
output stream of bytes (1) the file
Here Insert Picture Description
(2) the output byte stream file
Here Insert Picture Description
byte (3) use of a file copy operation is completed the flow
Here Insert Picture Description
(4) automatically close the resources provided by way Java7
Here Insert Picture Description
(5 ) character output stream file
Here Insert Picture Description
concept buffering:
flush (refresh) operation, the output stream are flush method:
computer access to external devices (disk file), direct memory access is much slower than if each should write directly to write to a disk file, CPU will spend more time, then we can prepare a memory buffer, each time the program write methods are written directly to the memory buffer, when the memory buffer is full, the system only then buffer the contents of a one-time write to a disk file.
the benefits of using buffer:
1: increase CPU usage.
2: a chance to roll back the written data.
for byte stream, flush method is not every role (only part of the byte stream the role of buffer stream) for the stream of characters are working.
If we call the close method, system resources before closing, will first call the flush method.
the operating system uses the closing tag of -1 indicates a disk file.
buffer size typically used capacity integer multiple IO performance can be improved.

Here Insert Picture Description
(6) Packaging flow and the buffer flow overview
process flow / packet stream (relative to the stream of nodes higher) decorative design pattern / Packing Mode:
1: hides the differences underlying nodes stream, and external to provide a more convenient input / output function, so that we only care about operation advanced stream.
2: process flow package node stream, to operate directly with the processing flow, so that the flow from the underlying device nodes do IO operations.
3: process flow can simply close
the packaging stream how to distinguish: write code and found objects to create objects when you need to pass another stream object.
new new packaging stream (stream object);

What is buffered stream:
a stream of packaging, the purpose of acting as a buffer.
BufferedInputStream:
BufferedOutputStream:
BufferedReader:
BufferedWriter:

Object stream buffer:
when operational flow, used to define a byte / char array.
Int Read (): each time a byte is read from the disk file the disk file is very low direct operating performance.
Solution: define an array as a buffer
byte [] buffer = new byte [ 1024];. the buffer array is actually a
one-time read from the disk file in bytes so since 1024, the number of operations less disk file, ---. -> performance can be improved.
Since we can think of, SUN company has long been thought to provide a default buffer size is 8192 (1024 * 8), we generally do not change the size.
Here Insert Picture Description
Here Insert Picture Description
(7) character buffer flow
Here Insert Picture Description
(8) commutations memory stream and
converting the stream: the character stream into a byte-stream:
the InputStreamReader: byte input flow into the character input stream
the OutputStreamWriter: the output stream of bytes to the output stream of characters.

Why turn byte character stream, no character transfer byte stream.
Byte stream can operate all file (plain text file / binary files).
Character stream is used to operate the Chinese use the plain text itself is a stream of bytes enhancement.

Memory stream (stream array): adapter mode :( expand knowledge)
the first interim data array, will be re-acquired from the array exists.
1): byte memory stream: ByteArrayInputStream / ByteArrayOutputStream
2): Character memory stream: CharArrayReader from / CharArrayWriter with
. 3): string stream: StringReader / StringWriter (the data is temporarily stored in the string)

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
(9) The combined flow
combined stream / sequence flow (SequenceInputStream):
that is, the plurality of input streams into a combined stream object.
Here Insert Picture Description

(10) print stream
print stream, the print data, the print stream is an output stream only:
PrintStream: byte stream print
PrintWriter: character print stream
- for PrintWriter is, when the refresh enable field,

Or call println printf, or format methods will immediately refresh operation.
If you do not turn on automatic refresh, you need to manually refresh or when the buffer is full, and then automatically refresh.
Use the print stream as the output stream, then the output operation will be particularly simple, since the print stream:
providing a print: Print not wrap
provides println: Print line back to
print and print method println support / outputs various data types, remember void println (Object x) to . Here Insert Picture Description
Here Insert Picture Description
(11) standard IO overview and
standard IO:
standard input: keyboard input data to the program.
standard output: data on the screen display program.

System class has two constants:
the InputStream in = the System.in;
PrintStream the System.out = OUT;

Redirect the standard operation of the stream:
standard input: keyboard input data to the program.
Source reassign the keyboard input is no longer, but a file.
Static void setIn (in the InputStream) redistribute the "standard" input stream.
Thereafter, the source System.in data is developed by setIn sources
standard output: display the program data on the screen.
Goal is no longer to reassign the output screen, instead of a file.
Static void setOut (PrintStream OUT) reallocation "standard" output stream.

Here Insert Picture Description

Here Insert Picture Description
(12) the data stream
the data stream, a read / write arbitrary data types of methods:
the DataOutputStream: Providing writeXxx (xxx value) method.
DataInputStream:. Providing readXXX () Method
Note: writeXxx and readXXX must be correspondence, writeByte write data, only this time using readByte read back.
Here Insert Picture Description
(13) pipeline flow
duct flow: implementing data interaction between the two threads.
the PipedInputStream
the PipedOutputStream
PipedReder
PipedWriter

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Six .NIO
NIO: New IO:
From the beginning JDK1.4 proposed new IO, can map a disk file into memory, we could read the data in memory.
Stored in java.nio package.
The Java NIO (new IO) IO API is a new Java 1.4 version from the beginning of the introduction, you can replace the standard Java IO API
is now used primarily for servers, for we still write code using traditional IO is enough.
in JDK1.7 extract the updated IO, NIO2.0 .-> files (file tools).

Here Insert Picture Description
Seven .IO flow summary
Here Insert Picture Description

Published 99 original articles · won praise 2 · Views 2595

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105316414