Concept of stream
STRAM's concept derived from the concept of Pipes (PIPE) in UNIX. In UNIX, the pipe is an uninterrupted byte stream for communications between programs or processes, or reads and write peripherals, external files, and the like. A stream, there is a source and destination, which can be some areas of computer memory, or can be a disk file, or even a URL on the IRNET. The direction of the stream is important. According to the direction of the stream, the stream can be divided into two categories: input flow and output flow. Users can read information from the input stream, but cannot write it. Instead, the output stream can only be written to the input, and it cannot be read. In fact, the source and destination of the stream can simply see the producer and consumers of the byte, the input stream, but not to care about it, just read data from the stream, but Output stream, you can also know its destination, just write data simply in the stream.
The class in the java.io package corresponds to the two types of streams, and a class of streams are read or written directly from the specified location (such as disk file or memory area). This type of stream is named Node Stream, and other flow is said For the filter (Filters). Filter input flow is often provided by other input streams as its input source, and after filtering or processing, it is also available to the user in the form of a new input stream. The principle of filter output stream is similar.
Java's common input, output flow
In Java, the program can open an input stream, the input stream can be located in the file, memory, or network socket (socket) and other places, the type of information can be included in the object, characters, images, sounds Any type. Once the input stream is turned on, the program can read the data from the input stream. The process of reading data from input data is generally as follows: Open A StreetWhile More Information Read InformationClose The Stream
Similarly, the program can also send information to the destination by opening an output stream and sequentially writing data. The procedure for outputting data is generally as follows: Open A StreetWhile More Information Write InformationClose The Stream
The Stream class in the java.io package is character or bytes according to the type of operation objects, and can be divided into two categories: character flow and byte stream.
Java's byte stream
It can be seen that InputStream is the ancestors of all byte input streams, while OutputStream is the ancestors of all byte output streams. InputStream This class is an abstract class, which includes: int () int in (byte []) int in (byte [], int, int, int, int
These three methods can read bytes or bytes from the input stream. It is to be noted that the non-parametric READ () method returns an integer or -1, and -1 represents the end of the stream, and the other to read bytes. The latter two methods read the byte to the parameter, the return value is the number of bytes or -1 actually read (the end is encountered). The latter two parameters of the third Read method give a maximum number of bytes read in the read starting position and read. InputStream also has some other methods, such as: Void Close () // Close the stream int available () // Report Direct readable byte number Skip (long) // Skip the byte specified in the stream
OutputStreamOutputStream is also an abstract class. Its main way includes: void write (int) void write (byte []) void write (byte [], int, int, int)
The INT type parameters of the first method correspond to the byte to be written, and the parameters of the latter two methods are similar to the InputStream. Void Close () // Turns the output stream void flush () // Forcibly write the remaining data in the write buffer to fileInputStream and FileOutstream These two classes belong to the node stream, the source and the second source of the first class and the second The destination of the class is a disk file, and their constructor allows the corresponding stream to be constructed by the path name of the file. Such as: fileinputstream infile = new fileinputstream ("myfile.dat"); fileOutputStream outfile = new fileoutputstream ("results.dat");
It should be noted that constructing the fileInputStream, the corresponding file must exist and readable, and when the fileOutputStream is constructed, if the output file already exists, it must be overwritten.
BufferedInputStream and BufferedoutputStream They are filters flow, and their role is to improve the efficiency of the input and output.
DataInputStream and DataOutputStream These two class created objects are called data input streams and data output streams, respectively. This is a very useful stream that allows the program to read and write Java data in style with the machine-independent style. So compare data transmission on the network. These two streams are also filter streams, often used in other streams such as InputStream or OutputStream as their input or output.
Java character flow
The character stream is mainly used to handle characters. Java uses a 16-bit Unicode to represent strings and characters, and the corresponding character stream is called Readers and Writers, respectively, respectively.
InputStreamReader and OutputStreamwriter are automatically converted when constructing these two classes, and converts the bytes of the platform default encoding set to Unicode characters. For an English environment, its default encoding set is generally ISO8859-1.
BufferedReader and BufferedWriter These two class corresponding streams use buffers that greatly improve the efficiency of the input and output. These two are also filter flows, often used to process the InputStreamReader and OutputStreamWriter. Such as: import java.io *; public class Echo {public static void main (String [] args) {BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String s; try {while ((s = in. .readline ()). Length ()! = 0) System.out.println (s); // an Empty line Terminates the program} catch (ioException e) {E.PrintStackTrace ();}}}
The program accepts the keyboard input and looks back.