java_day11_IO flow

Chapter XI: IO streams

1. The concept of stream
flow is an abstract concept, abstract input and output device, Java programs for data input / output operation are the "flow" manner. Device can be a file, network, memory, etc.

Directional flow, as is the input stream or output stream is a relative concept, generally referenced procedure, if the data flow is to program the device, we have an output stream, if the flow of data is called an input device to the program flow.

In the form of binary data flow transmitted between procedures and equipment, as wanted water flows in the pipeline, it is brought in the data transfer mode called the input stream output stream

Thinking:? Code program to read the contents of files, use the code stream of what the program would like to write the contents of a file, you need to use what stream?


2. Flow Classification
1) in the direction of flow into the input and output streams
2) according to different processing unit data character stream into a byte stream and
the smallest unit is a byte stream read byte (1byte = 8bit ), the character stream can be read once a character (1char = 2byte = 16bit)
. 3) that differ in function and the process flow stream into node
flow stream node can be "directly" read and write data from a data source.
Function may also be called a process flow stream or a packet stream, it may be made of a streaming node package stream, the package may increase the functionality of the flow nodes.
For example: FileInputStream stream is a node, the data can be read directly from the file, and the FileInputStream BufferedInputStream may be packaged, so that it has a function of buffering data.

4) In addition to the above three categories, there are other types of: stream objects, the buffer flow, the compressed stream, the stream file, etc., in fact, these are the nodes or the process stream flow subcategories. Of course, it may also be divided in other types of flow, if so desired.

5) No matter how rich and complex flow classification is rooted from the four basic parent
byte input stream: InputStream
byte output stream: OutputStream
character input stream: Reader
character-output stream: Writer

Note: This four parent class is an abstract class


3. The common node byte stream stream
NOTE: java io stream commonly used in java.io package
. 1) the InputStream
// Reads the next byte of data from the input stream
// If the end of the stream is return -1
public abstract int read ();
// save the read byte to the byte array b, and returns this number of bytes read
public int read (byte [] b) {..}
/ / read byte stored to the array b of bytes, specify the start position and the maximum number of bytes of memory, and returns this number of bytes read
public int read (byte [] b , int off, int len) {..}

// returns a stream input method calls input stream without blocking read (or skipped) estimates the number of bytes
public int Available () {..}

// skip this input n bytes of data in the stream of
public long skip (long n) { ..}

// Close this input stream and releases any system resources associated with the stream of
public void Close () {..}

// test this input stream supports the mark and reset methods
public Boolean markSupported () {..}
// here input stream marks the current position
public void mark (int readlimit) {..}
// reposition this stream to the last position at this input stream mark method was called
public void reset () {..}

2) the OutputStream
// the specified byte to this output stream
public abstract void Write (int b);
// b all the bytes in the byte written to this array output stream
public void write (byte [] b ) {..}
// byte array b byte to this output stream, designated start position and the maximum number of bytes
public void write (byte [] b , int off, int len) {..}

// Flushes this output stream and forces all buffered output bytes to write
public void flush () {..}

// Close the output stream and releases any system resources associated with the stream
public void Close () {..}


. 3) the InputStream and OutputStream subclasses subclasses are almost pairs, the read data is responsible for a , a data write operation is responsible


4) System.out and System.in
portion Source System class:
public class System Final {
// standard input stream
public static Final in the InputStream = null;
// standard output stream.
OUT = Final static PrintStream public null;
// standard error output stream
public final static PrintStream err = null;

public static void setIn(InputStream in){..}
public static void setOut(PrintStream out){..}
public static void setErr(PrintStream err){..}
}

Default standard input stream to read data from the console
standard output stream will default to output the data to the console

System.out.println(System.in.getClass());
System.out.println(System.out.getClass());
输出结果为:
class java.io.BufferedInputStream
class java.io.PrintStream


5) ByteArrayInputStream and ByteArrayOutputStream
A ByteArrayInputStream can be read from the byte array
ByteArrayOutputStream byte can be written in an object buffer which, in fact, a byte array

. 6) and FileInputStream FileOutputStream
bytes read FileInputStream file
FileOutputStream byte to be written into the file

7) PipedInputStream PipedOutputStream and
the PipedInputStream pipe input stream of bytes
PipedOutputStream byte output stream conduit

Note: You need both a pipeline to use for docking

8) ObjectInputStream ObjectOutputStream and
objects to be used in the sequence of the input stream and output stream objects, using the test again after

9) java.io.File class
File represent a type of object can also represent a file directory.


4. The process stream used in the byte stream
may also be referred to as a stream or packet stream function, as it is packaged with a fluid flow nodes, the package may increase the functionality of the flow nodes. However, the processing flow itself is not directly read and write data, and

1) BufferedInputStream BufferedOutputStream and
may provide code to the nodes in the stream buffer byte stream input / output stream
2) DataInputStream and DataOutputStream
can provide input / output java different types of stream data to byte stream node
3) PrintStream
PrintStream adds functionality to another output stream, namely the ability to print various data values in the form of


5.字符流
1)Reader
public int read(){..}
public int read(char[] cbuf){..}
public abstract int read(char[] cbuf, int off,int len){..}
//指定缓冲区
//@since 1.5
public int read(CharBuffer target){..}

abstract public void close();
public long skip(long n){..}

public boolean markSupported(){..}
public void mark(int readAheadLimit){..}
public void reset(){..}

//Tells whether this stream is ready to be read
public boolean ready(){..}

2)Writer
public void write(int c){..}
public void write(char cbuf[]){..}
abstract public void write(char cbuf[], int off, int len);
public void write(String str){..}
public void write(String str, int off, int len){..}

abstract public void flush();
abstract public void close();

//@since 1.5
//和out.write(c)的效果一样
public Writer append(char c){..}
public Writer append(CharSequence csq){..}
public Writer append(CharSequence csq, int start, int end){..}


3) CharArrayReader and CharArrayWriter
CharArrayReader can read the contents of the character array
CharArrayWriter can write content to the character array

4) FileReader and FileWriter
the FileReader Convenience class reading file content, InputStreamReader subclass
subclass FileWriter Convenience class to write the file content, OutputStreamWriter of

. 5) and PipedReader PipedReader
PipedReader pipe character input stream
PipedReader pipe character output stream

6) BufferedReader BufferedWriter and
this belongs to both a process flow stream, and can not read the data themselves, their role in the packaging of other nodes in the flow above, to provide additional functionality

. 7) PrintWriter
general will BufferedReader and PrintWriter used together with, as a read row can BufferedReader strings, a string of one line can be written PrintWriter (wrap).


6. Convert streams
InputStreamReader and OutputStreamWriter is a pair of names in both the Stream, there are flow Reader or Writer, as they are converted stream, responsible for a byte stream into a character stream. They bridge between the byte stream and string.
Note: In the process of the byte stream into a character stream, but also can specify the character encoding, to avoid occurrence of distortion.

8. A stream of objects
1) and deserializing serialized
serialized Java means converts the Java object sequence of bytes
Object Serialization --- ---> 01010101
deserialization in Java refers to the word Day sequence recovery process Java objects
01010101 --- --- deserialized> object

thinking: Why do we need serialization and de-serialization?


2) how to serialize and deserialize
used to achieve the object stream object serialization and deserialization
method ObjectOutputStream class complete serialized object:
public void Final the writeObject (Object obj) {..}

The method can be accomplished ObjectInputStream class object to deserialize:
public the readObject Final Object () {..}


NOTE: maybe stream objects belong byte stream

3) serialization requirements
only achieved java.io.Serializable interface object class can be serialized, otherwise it will error when serializing

thinking: What is the role of the test sequence the version number is?

4) transient keyword
in the subject serialization when the transient modified value of the property, in the serialization process will be neglected.

9. The random access stream
java.io.RandomAccessFile class
public class RandomAccessFile extends Object {..}
This is a special stream, which does not belong to the system prior to that stream.

This flow may be used to read a file, the file can be used to write the content and methods in the class file can be used in the position location:
public void Seek Native (Long POS);


Constructor needs to set the operating mode of the stream:
// read-only file
a RandomAccessFile a RandomAccessFile R & lt new new = (filePath, "R & lt");
// file to read and writeable
// but once read or write is determined, then it can not be changed
RandomAccessFile rw = new RandomAccessFile (filePath, "rw");

 

Guess you like

Origin www.cnblogs.com/yue-170305/p/11478935.html