Java I/O Stream

Source: Internet
Author: User
The concept of stream is derived from the concept of pipe in UNIX. In UNIX, a media transcoding queue is an uninterrupted byte stream used for communication between programs or processes, or reading and writing peripheral devices and external files.
A stream must be a source end and a destination end. It can be a certain area of computer memory, a disk file, or even a URL on the Internet.
The direction of the stream is important. Based on the direction of the stream, the stream can be divided into two types: input stream and output stream. You can read information from the input stream but cannot write it. On the contrary, the output stream can only be written to the input stream, but cannot be read.
In fact, the source and destination of a stream can be viewed as the byte producer and consumer. You do not have to worry about the source of the input stream, as long as data is read from the stream, but the output stream does not know its target end, it simply writes data to the stream. Java. the classes in the IO package correspond to two types of streams. A type of stream reads or writes directly from a specified location (such as a disk file or memory area). Such streams are called node streams ), other streams are called filters ). The input stream of the filter is often used as the input source of another input stream. After filtering or processing, it is provided to the user in the form of a new input stream. The principle of the output stream of the filter is also similar.
Java's common input and output streams are in Java. The program can open an input stream. The source of the input stream can be located in files, memory, network sockets, and other places, information sources can be any type including objects, characters, images, and sounds.
Once the input stream is opened, the program can read data from the input stream in sequence.
The process of reading data from an input stream is generally as follows:
Open a stream
While more information
Read Information
Close the stream Similarly, the program can also send information to the target end by opening an output stream and writing data in sequence.
 
The process of writing data to the output stream is generally as follows:
Open a stream
While more information
Write Information
The stream class in the close streamjava. Io package can be divided into two categories based on whether they operate on the object type is character or byte: dense stream and word throttling.
The byte stream of Java shows that inputstream is the ancestor of all byte input streams, while outputstream is the ancestor of all byte output streams.
Inputstream
This class is an abstract class and its methods include:
Int read ()
Int read (byte [])
The Int read (byte [], Int, INT) methods can be used to read byte or byte arrays from the input stream. Note that the non-parameter read () method returns an integer or-1 between 0 and 255.-1 indicates that the stream ends and other corresponding bytes are read.
The last two methods read the bytes into the byte array specified by the parameter. The returned value is the number of bytes actually read or-1 (when the stream ends ). The last two parameters of the third read method provide the starting position for reading and the maximum number of bytes for reading.
Inputstream also has some other methods, such:
Void close () // close the stream
Int available () // number of bytes that are directly readable in the report stream
Skip (long) // specifies the byte outputstream In the hop-over-current.
Outputstream is also an abstract class. Its main methods include:
Void write (INT)
Void write (byte [])
Void write (byte [], Int, INT) the int type parameter of the first method corresponds to the byte to be written, and the parameters of the last two methods are similar to that of inputstream.
Void close () // close the output stream
Void flush () // forcibly write the remaining data in the buffer to fileinputstream and fileoutputstream
These two classes belong to the node stream. The source and destination of the first class are both disk files, and their constructor allows the file path to construct the corresponding stream. For example:
Fileinputstream infile = new fileinputstream ("myfile. dat ");
Fileoutputstream OUTFILE = new fileoutputstream ("results. dat "); note that when constructing a fileinputstream, the corresponding file must exist and be readable. When constructing a fileoutputstream, if the output file already exists, it must be overwritten. Bufferedinputstream and bufferedoutputstream
They are filter streams, which improve the efficiency of input and output. Datainputstream and dataoutputstream
The objects created by these two classes are called data input streams and data output streams respectively. These are two useful streams that allow programs to read and write Java data in a machine-independent style. Therefore, it is suitable for data transmission over the network. These two streams are also filter streams, and other streams, such as inputstream or outputstream, are often used as their input or output.
Java's character streams are mainly used to process characters. Java uses 16-bit Unicode to represent strings and characters. The corresponding bytes stream is called readers and writers respectively by input and output. Inputstreamreader and outputstreamwriter
When constructing the streams corresponding to these two classes, they are automatically converted to unicode characters. For English environments, the default limit set is generally ISO8859-1. Bufferedreader and bufferedwriter
The stream corresponding to these two classes uses buffering, which can greatly improve the efficiency of input and output. These two filters are also used to process inputstreamreader and outputstreamwriter. For example:
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 keyboard input and displays it back. For the bufferedreader class, the Readline () method of this class can read a row from the stream at a time, but for the bufferedwriter class, there is no way to write a row at a time, therefore, if you want to write a row to the stream at a time, use the printwriter class to transform the original stream into a new print stream. The printwriter class has a method println (), which can output one row at a time. For example:
............
Printwriter out = new printwriter (New bufferedwriter (
New filewriter ("D:/javacode/test.txt ")));
Out. println ("Hello world! ");
Out. Close ();
............

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.