How many types of streams are available in Java? JDK provides some abstract classes for each type of stream. which classes are they?

Source: Internet
Author: User
Tags object serialization
There are many other streams in the Java. Io package, mainly to improve performance and ease of use. C/C ++ can only provide byte streams. In Java, streams are divided into two types: byte streams and merge streams, which are represented by four abstract classes respectively (each type of stream includes two types of inputs and outputs, so there are four in total ): inputstream, outputstream, reader, and writer. In Java, other diverse stream types are derived from them.



Traffic streams and word throttling are differentiated based on data processing. Byte streams are transmitted in 8 bits. byte streams are the most basic and all files are stored in bytes, saving on a disk is not a file character, but encoding the character into bytes before storing these bytes to the disk.


1. byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings;


2. the byte stream provides the ability to handle any type of Io operations, but it cannot directly process Unicode characters, and the bytes stream can.


When reading text, use a text stream, such as a TXT file. When reading non-text files, use Word throttling, for example, MP3. Theoretically, any file can be read with word throttling, but when reading text data, you must go through another conversion process to restore the file to text, the merge stream saves the trouble and can be directly read.


The Bytes stream processing unit is two bytes of Unicode characters, namely, manipulating characters, character arrays or strings, while the byte stream processing unit is one byte, manipulating byte and byte arrays. Therefore, the compaction stream is a character that is converted from a Java virtual machine to a Unicode Character in two bytes. Therefore, it is highly supported by many languages!

1. byte stream: continues with inputstream \ outputstream.

Methods provided by outputstream:


Void write (int B): write data into a byte.


Void write (byte [] buffer): writes the data in the array buffer to the stream.


Void write (byte [] buffer, int offset, int Len): writes data in len bytes starting from buffer [offset]


Void flush (): forcibly writes data in the buffer into the stream.


Void close (): Closed stream


Inputstream provides the following methods:


Int read (): reads a byte of data. If the data reaches the end of the file, the returned value is-1.


Int read (byte [] buffer): reads data of the buffer size. The returned value is the number of bytes actually read.


Int read (byte [] buffer, int offset, int Len)


Int available (): returns the number of bytes that can be read in the stream.


Long SKIP (long n): Skips n bytes of data. The returned value is the number of actually skipped data.


Void close (): Closed stream

2. the upload stream continues with inputstreamreader \ outputstreamwriter.

The following is the class of the primary stream: 1). bufferedreader is a filter (extends filterreader ). Filter


It is used to process and output the stream data. The constructor is:


Bufferedreader (Reader in): A natural buffer character is input to the stream, and in is a reader.


Bufferedreader (Reader in, int size): A natural buffer character is input to the stream, and the buffer size is specified as size.


Public class iostreamdemo {

Public void samples () throws ioexception {// 1. This is a line of data read from the keyboard and returns a string

Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));

System. Out. Print ("enter a line :");

System. Out. println (stdin. Readline ());


// 2. This is to read data row by row from the file


Bufferedreader in = new bufferedreader (New filereader ("iostreamdemo. Java "));

String S, S2 = new string ();

While (S = in. Readline ())! = NULL)

S2 + = S + "\ n ";

In. Close ();


// 3. This is to read bytes one by one from a string.

Stringreader in1 = new stringreader (S2 );

Int C;

While (C = in1.read ())! =-1)

System. Out. Print (char) C );


// 4. This is to write a string into the file

Try {

Bufferedreader in2 = new bufferedreader (New stringreader (S2 ));

Printwriter out1 = new printwriter (New bufferedwriter (New filewriter ("iodemo. Out ")));

Int linecount = 1;

While (S = in2.readline ())! = NULL)

Out1.println (linecount ++ ":" + S );

Out1.close ();

} Catch (eofexception e ){

System. Err. println ("End of stream ");

}

}}


For the above example, we need to explain the following points:


1. inputstreamreader is a bridge between inputstream and reader. Because system. In is a byte stream, it needs to be packaged and then changed to a bytes stream for bufferedreader.


3. printwriter out1 = new printwriter (New bufferedwriter (New filewriter ("iodemo. Out ")));


This statement represents a feature of the JAVA input/output system. for a specific purpose, several layers need to be packaged. First, the output destination is the file iodemo. so filewriter is encapsulated in the innermost layer to create an output file stream. Next, we want to see that the stream is buffered. Therefore, bufferedwriter is used to package it for the purpose. Finally, we need to format the output result, so we put the printwriter package in the outermost layer.


Java streams have another important purpose, that is, serialization of objects using object streams.


When a program runs, the variable data is stored in the memory. Once the program ends, the data will not be saved. One solution is to write the data into the file, java provides a mechanism to write the objects in the program into the file, and then read the objects from the file and recreate them. This is the so-called Object serialization. It is introduced in Java mainly for RMI (Remote Method Invocation) and Java Bean, but it is also a useful technology in common applications.

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.