The difference between stream,reader/writer,buffered (1)

Source: Internet
Author: User

Stream: is the byte stream form, exe file, picture, video etc. Supports 8-bit characters for ASCII characters and binary data.

Reader/writer: is a character stream, text file, Xml,txt, etc., for 16-bit characters, which is Unicode.

Buffered: put it in the buffer to improve the efficiency of read and write, to provide an efficient way to read. InputStream's read will clog up.

InputStream:

Subclass of InputStream:

1.ByteArrayInputStream

就是因为InputStream is not cached and then read repeatedly. So把字节流数组保存起来。为后期使用。

开发文档中的介绍:

ByteArrayInputStreamContains an internal buffer that contains the bytes read from the stream.

The internal counter trace read method to provide the next byte. Closing bytearrayinputstream is not valid.

The methods in this class can still be called after the stream is closed, without generating any ioexception.

Construction Method Summary
Bytearrayinputstream (byte[] buf)
Create a bytearrayinputstream, using BUF as its buffer array.
Bytearrayinputstream (byte[] buf, int offset, int length)
Create Bytearrayinputstream, using BUF as its buffer array.

Use:

InputStream is not cached and then read repeatedly.

Inside the program, create a buffer of a byte type array, and then write or read byte data to the array using instances of Bytearrayoutputstream and Bytearrayinputstream.

In the network transmission we often have to transfer a lot of variables, we can use Bytearrayoutputstream to collect all the variables together, and then send the data at once.

2.FileInputStream

以字节为单位读取文件,常用于读二进制文件,片、声音、影像等文件。

FileInputStream is used to read binary data, and FileReader is used to read character data.

开发文档中的介绍:

Gets the input bytes from a file in the file system. Which files are available depends on the host environment.

FileInputStreamUsed to read raw byte streams such as data. To read a character stream, consider using the FileReader .

Construction Method Summary
FileInputStream (File file)
Create a FileInputStream by opening a connection to the actual file, which is specified through the file-object files in the filesystem.
FileInputStream (FileDescriptor fdobj)
Create a FileInputStream by using the file descriptor Fdobj, which represents an existing connection to an actual file in the file system.
FileInputStream (String name)
Create a FileInputStream by opening a connection to the actual file, which is specified by the pathname name in the file system.

Use:

Read the picture through FileInputStream and save it to the database (in the form {f4f472d476f6f67} in the database) and then read it and display the picture on the screen.

Instance:

ImportJava.io.*; Public classFilestreamdemo { Public Static voidMain (string[] args)throwsIOException {//Create two files, Face.gif is already existing file, Newface.gif is newly created fileFile InFile =NewFile ("Face.gif"); File OutFile=NewFile ("Newface.gif");//Create a stream file (binary data for a picture) to read into and write out a classFileInputStream instream =NewFileInputStream (InFile); FileOutputStream OutStream=NewFileOutputStream (outFile);//the maximum number of characters in a stream obtained by the available methodbyte[] Inoutb =New byte[Instream.available ()];instream.read (INOUTB); //read stream, saved in byte arrayOutstream.write (INOUTB);//write the stream and save it in the file Newface.gifinstream.close (); Outstream.close ();}}

3.BufferedInputStream

With and ByteArrayInputStream 比构造函数不同, can initialize an internal array of the corresponding size according to the specified sizes. Specify the buffer size this is a very obvious advantage. Use the same. A byte stream file for a slightly larger point.

开发文档中的介绍:

BufferedInputStreamAdds some functionality to another input stream, which is the ability to buffer input and support mark and reset methods.

When created BufferedInputStream , an array of internal buffers is created. When you read or skip bytes in a stream, you can populate the internal buffer again from the included input stream as needed, filling in more than one byte at a time.

markThe action records a point in the input stream, which reset causes mark all bytes read after the last operation to be read again before the new byte is fetched from the included input stream.

Construction Method Summary
Bufferedinputstream (InputStream in)
Create a Bufferedinputstream and save its arguments, which is input stream in for future use.
Bufferedinputstream (inputstream in, int size)
Creates a bufferedinputstream with the specified buffer size and saves its arguments, that is, input stream in for future use.

Use:

Uses the same as Bytearrayinputstream. A byte stream file for a slightly larger point.

4.DataInputStream:

Read in a different format. That is, data streams that can identify different data types.

That

Readdouble ();

Readboolean ();

ReadInt ();

form to read.

开发文档中的介绍:

The data input stream allows the application to read basic Java data types from the underlying input stream in a machine-independent manner. The application can use the data output stream to write data that is later read by the data input stream.

DataInputStream is not necessarily secure for multithreaded access. Thread safety is optional and is the responsibility of the user of this class of methods.

Construction Method Summary
DataInputStream (InputStream in)
Creates a datainputstream using the specified underlying InputStream.

Use:

The following form will be used when going to read

... {...//write data to a carrierDataOutputStream out =NewDataOutputStream (NewFileOutputStream ("Order.txt"));//PriceDoubleprices[]={10.10,10.20,8.30};//numberint[] num={10,20,5};//DescriptionString desc[]={"Hat", "belt", "glasses"};//Write Data for(inti=0;i<prices.length;i++) {out.writedouble (prices[i]); Out.writechar (' \ t ');  Out.writeint (Num[i]); Out.writechar (' \ t ');  Out.writechars (Desc[i]); Out.writechar (' \ n ');} ...

5.PipedInputStream:

Known as pipeline flow, is the transfer of data between different threads, which can be separated from the read operation and write operations within the bounds of the buffer.

开发文档中的介绍:

The pipeline input stream should be connected to the pipeline output flow, and the pipeline input stream provides all the data bytes to write to the pipeline output flow.

Typically, data is read from an object by a thread PipedInputStream and written by another thread to the corresponding PipedOutputStream . It is not recommended that you try to use a single thread for both objects, because this could be a deadlock thread.

The pipeline inflow contains a buffer that can be separated from the read and write operations within the bounds of the buffer. If the thread that provides data bytes to the connection pipeline output stream no longer exists, the pipeline is considered corrupted.

Construction Method Summary
PipedInputStream ()
Create PipedInputStream that are not yet connected.
PipedInputStream (int pipesize)
Creates a pipedinputstream that is not yet connected and uses the specified pipe size for the pipe buffer.
PipedInputStream (PipedOutputStream src)
Create the PipedInputStream so that it connects to the pipe output stream src.
PipedInputStream (pipedoutputstream src, int pipesize)
Creates a pipedinputstream that connects to the pipe output stream src and uses the specified pipe size for the pipe buffer.

Use:

Pipeline flows are primarily used for communication between threads, and a thread's PipedInputStream object reads data from a PipedOutputStream object on another thread. For the pipeline to be useful, you must build both the pipeline inflow and the pipeline output stream.

Instance:

ImportJava.io.PipedInputStream;ImportJava.io.PipedOutputStream;classpipedstreamtest{ Public Static voidMain (string[] args) {PipedOutputStream pos=NewPipedOutputStream (); PipedInputStream PiS=NewPipedInputStream (); Try{pos.connect (PiS); NewProducer (POS). Start (); NewConsumer (PiS). Start (); }Catch(Exception e) {e.getstacktrace (); } }}classProducerextendsthread{PrivatePipedOutputStream POS; Producer (PipedOutputStream pos) { This. pos =POS;}  Public voidrun () {Try{pos.write ("Hello,world!". GetBytes ());  Pos.close (); }Catch(Exception e) {e.getstacktrace (); } } }classConsumerextendsthread{PrivatePipedInputStream PiS; Consumer (PipedInputStream PiS) { This. PiS =PiS;}  Public voidrun () {Try{   byte[] buffer =New byte[100]; intLen =pis.read (buffer); System.out.println (NewString (buffer,0, Len));  Pis.close (); }Catch(Exception e) {e.getstacktrace (); } }}

6.StringBufferInputStream:

This class is out of date, don't use this.ByteArrayInputStream

OutputStream

OutputStreamSub-class:

The outputstrem here is corresponding to the previous inputstrem.

The only difference is that Inputsream is about the read operation, and Ouputstream is about the write operation.

Refer to the InputStream to understand the line.

The next section goes on to talk about Reader/writer:

The difference between stream,reader/writer,buffered (1)

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.