Purpose of writing
Self-taught Java, the concept ofJava Stream is more complex, so specifically for a collation. Expected to start learning children's shoes, after reading this article on the Java Stream has a general understanding. Mainly divided into three small articles:
I. Overview, input / output byte stream
Input / output character stream, decorator mode and processing flow
Third, other
In this paper, we introduce the concept and classification, then introduce the interface of the byte stream, finally introduce some three kinds of commonly used byte stream, and illustrate their usage.
One, what is the flow
First, give a picture, give everyone a perceptual understanding. Everyone is exposed to object-oriented languages at least, and when it comes to input and output (I/O) in object-oriented languages, it is generally a stream. So what is a flow? Can you describe it exactly? In fact, I can not accurately describe the description, the following is my understanding. First of all , io,io is the transfer of data from one place to another, such as from a file to the memory, from one machine to another machine. Again, the flow is an object that hides the details of the handling (or encapsulates the handling details). As shown, the stream object is like a bridge, where the source data and destination are connected, and we programmers do nothing, as long as the source data is stored in the stream object in the format specified by the stream object or read from the stream object.
TwoJavaThe flow in
Every learning a programming language, I/O is always a headache, although I have been writing code in C + + , but I do not understand the flow of C + + . Like control structures, basic data types, operation types, and so on, are generally the same, but streams can vary greatly in different languages. Because it is self-study, so make it clear some. The JDK itself provides a rich stream type. We'll start by using mind mapping to sort these classes:
is not very spectacular, first of all, the Java stream Classification:
-
is divided into input and output streams by flow direction (input / output is for cpu );
-
According to the type of processing data, byte stream and character stream (general byte stream is sufficient, involving text encoding, especially non-ascii encodes a character stream. Here, one thing to emphasize: all the data stored on the disk is byte-oriented, and the character exists only in the program (or memory).
- by function, can be divided into node stream and processing flow. Node streams provide basic functionality, and processing streams provide extended functionality to increase performance.
Of all these classes, the most important are InputStream and OutputStream, followed by Reader and Writer. First, the following InputStream interfaces are described in detail.
Interfaces provided by the 2.1 InputStream class
Interface one, reads a byte of data from the stream
The above method is InputStream 's only abstract method, which must be implemented by subclasses, whose function is to read into one byte of data. Different input streams (with different data sources), the way to read them is of course not the same, and the next few interfaces to be introduced depend on this interface. There are two possible return values for this interface:(1) read the value of the byte ; (2)-1 indicates that the end of the stream has been read.
Interface two, read into a number of bytes from the stream
This function reads a number of bytes from the stream into the cache array b[] , and the return value also has two cases:
(1) the number of bytes actually read into byte array b (possibly <b.length);
(2)-1, has reached the end of the stream, no bytes to read!
In general, the above two functions are the two most common functions we use when using an input stream. In addition, theJDK gives us another more flexible interface for reading into several bytes:
This interface is similar to the functionality of the previous interface, except for the addition of the off and Len two parameters, allowing the user to specify the starting position and length of the read-in byte. If len + off > B.length, a indexoutofboundsexception exception is thrown.
Interfaces provided by the 2.2 OutputStream class
Next, describe the interface for the output stream. outputstream Interface with inputstream The interface of the corresponds to one by one. You have read one byte , then I have write A byte; you have read a few bytes, then I have write a number of bytes. The interface is as follows (not described in detail):
Public Abstract void Write (intthrows IOException; Public void Write (bytethrows IOException; Public void Write (byteintintthrows IOException;
derivative classes and functions of 2.3-byte stream
2.4 Use of Streams
Here are a few examples to look at the usage of the three streams described in section 3 .
1. File input stream
1 Public Static voidMain (String args[])throwsIOException {2 //define file input stream3FileInputStream FIS =NewFileInputStream ("Test.txt");4 5 //define read-in cache bytes6 byteBuf[] =New byte[1024];7 8 //read in using the Read interface9 Fis.read (BUF);Ten One //bytes of output read- in ASystem.out.println (NewString (BUF)); -}
Note: This reads the buf byte array, encodes it into a string string as the system default character set, and then outputs. If the character set encoding of the file is inconsistent with the system default character set encoding, you need to specify the encoding in the construction String , otherwise garbled characters may occur.
2. Byte array input stream
As noted earlier, this stream is primarily used to receive and parse network data, which is not meant to be on a single machine. In addition, it needs to be used in conjunction with the processing flow (e.g.,datainputstream), where we demonstrate it through a simple example.
1 Public Static voidMain (String args[])throwsIOException {2 //defines the input buffer, which is usually transmitted over the network. 3String Sbuf =NewString ("Bytearrayinputstream Test.");4 byteBuf[] =sbuf.getbytes ();5 6 //defining the input byte array stream7Bytearrayinputstream Bais =NewBytearrayinputstream (BUF);8 9 //read in 8 bytes and outputTen byte[] Obuf =New byte[8]; One Bais.read (OBUF); ASystem.out.println (NewString (OBUF)); - - //again: The above three lines of code only demonstrate that the general byte array stream is not used alone. the}
3. Pipeline Flow
Used in conjunction, it is best to use in the thread, facilitate the communication of threads. To demonstrate, first derive two thread classes, as follows:
1 //implementing two thread classes2 classThreadreadextendsThread {3 4 PrivatePipedInputStream MPis =NULL;5 6 PublicThreadread (PipedInputStream PiS) {7MPis =PiS;8 }9 Ten //reads the pipeline flow content and outputs One Public voidrun () { A byte[] buf =New byte[100]; - Try { - Mpis.read (BUF); the}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } +System.out.println (NewString (BUF)); - } + } A at classThreadwriteextendsThread { - -PipedOutputStream MPos =NULL; - - Publicthreadwrite (PipedOutputStream pos) { -MPos =Pos; in } - to Public voidrun () { +String s =NewString ("Hello!" PipedOutputStream. "); - Try{mpos.write (s.getbytes ()); the}Catch(IOException e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } - } the}
The main functions are as follows:
1 Public class PipedStreamDemo01 {
2 3 Public Static voidMain (String args[]) {4PipedOutputStream pos =NewPipedOutputStream ();5PipedInputStream PiS =NULL;6 Try {7PiS =NewPipedInputStream (POS);8}Catch(IOException e) {9 //TODO auto-generated Catch blockTen e.printstacktrace (); One } A -Thread Th1 =NewThreadread (PiS); -Thread Th2 =NewThreadwrite (POS); the - Th2.start (); - Th1.start (); -}
Third, summary
Here, the byte stream is roughly covered.
Java I/O Learning (i)