Classification and simple explanation of common streams in Java

Streams in Java refer to buffers that flow in computation.

The data flow from the external device to the central processing unit becomes the "input stream", and vice versa, it becomes the "output stream".

The main difference between character stream and byte stream:

 1. When a byte stream is read, a byte is returned when a byte is read; the character stream uses a byte stream to read one or more bytes (the number of bytes corresponding to Chinese is two, in UTF- 8 code table is 3 bytes). Check the specified encoding table first, and return the found characters.

 2. Byte stream can handle all types of data, such as: pictures, MP3, AVI video files, while character stream can only handle character data. As long as it is dealing with plain text data, it is necessary to give priority to using the character stream, and otherwise use the byte stream.

1. Byte stream:

Byte input stream classes: FileInputStream, BufferedInputStream and DataInputStream

FileInputStream: This class is used to read file contents from the local file system.

Construction method:

FileInputStream(File file): Opens a connection to the actual file to create a FileInputStream specified by the File object file in the file system.

FileInputStream(String name): Opens a connection to the actual file to create a FileInputStream specified by the pathname name in the filesystem.

Common method:

· int available(): Returns the estimated number of bytes remaining to read (or skip) from this input stream without blocking for the next method invocation on this input stream.

· void close(): Closes this file input stream and releases all system resources associated with the stream.

BufferedInputStream: This class itself has a buffer. When reading data, put it in the buffer first, which can reduce the access to the data source and improve the operation efficiency.

Construction method:

BufferedInputStream(InputStream in): Creates a BufferedInputStream and saves its parameters, the input stream in, for future use.

BufferedInputStream(InputStream in, int size): Creates a BufferedInputStream with the specified buffer size and saves its parameters, the input stream in, for future use.

Common method:

· int available(): Returns the estimated number of bytes remaining to read (or skip) from this input stream without blocking for the next method invocation on this input stream.

· void close(): closes this input stream and releases all system resources associated with the stream.

· int read(): Reads the next byte of data from the input stream.

·int read(byte[] b,int off,int len): Read each byte into the specified byte array starting at the given offset in this byte input stream.

 DataInputStream: This class provides some multi-byte-based read methods, which can read data of basic data types.

Construction method:

·DataInputStream(InputStream in): Creates a DataInputStream using the specified underlying InputStream.

Common method:

int read(byte[] b): Reads a certain number of bytes from the contained input stream and stores them into buffer array b.

· int read(byte[] b, int off, int len): Reads up to len bytes from the contained input stream into a byte array.

Byte output stream classes: FileOutputStream, BufferedOutputStream and DataOutputStream

FileOutputStream: This class is used to write data from files on the local file system.

Construction method:

FileOutputStream(File file): Creates a file output stream that writes data to the file represented by the specified File object.

FileOutputStream(String name): Creates an output file stream that writes data to a file with the specified name.

Common method:

·void close(): Closes this file output stream and releases all system resources associated with this stream.

·FileDescriptor getFD(): Returns the file descriptor associated with this stream.

·void write(byte[] b): Write b.length bytes from the specified byte array to the output stream of this file.

·void write(byte[] b, int off, int len): Writes len bytes from offset off in the specified byte array to the output stream of this file.

·void write(int b): Writes the specified bytes to the output stream of this file.

BufferedOutputStream: This class itself has a buffer. When writing data, it is first put into the buffer to realize the buffered data stream.

Construction method:

BufferedOutputStream(OutputStream out): Creates a new buffered output stream to write data to the specified underlying input stream.

BufferedOutputStream(OutputStream out,int size): Creates a new buffered output stream to write data with the specified buffer size to the specified underlying output stream.

Common method:

· void flush (): flush this buffered output stream.

·void write(byte[] b,int off,int len): Writes len bytes from offset off in the specified byte array to the output stream of this buffer.

·void write(int b): Writes the specified bytes to this buffered output stream.

DataOutputStream(OutputStream out): Create a new data output stream and write data to the specified base output stream.

Common method:

·void flush(): Empty this data output stream.

·int size(): Returns the current value of the counter written, that is, the number of bytes written to this data output stream so far.

·void write(byte[] b,int off,int len): Writes len bytes from offset off in the specified byte array to the underlying output stream.

·void write(int b): Writes the specified byte (the eight low-order bits of parameter b) to the underlying output stream.

2. Character stream:

FileReader: Convenience class for reading character files. The constructor of this class assumes that both the default character encoding and the default byte buffer size are appropriate.

Construction method:

FileReader(File file): Creates a new FileReader given the File from which to read data.

FileReader(String fileName): Creates a new FileReader given the filename to read data from.

The BufferedReader class is a subclass of the Reader class. It adds a character buffer to the Reader object, allocates memory storage space for data input, and accesses data more efficiently.

Construction method:

BufferedReader(Reader in): Creates a buffered character input stream using the default size input buffer.

BufferedReader(Reader in, int sz): Creates a buffered character input stream using an input buffer of the specified size.

Operation method:

· void close(): Closes the stream and releases all resources associated with it.

· void mark (int readAheadLimit): mark the current so far in the stream.

·boolean markSupported(); Determines whether this stream supports the mark() operation.

· int read (): read a single character.

· int read (char[] cbuf, int off, int len): read characters into a part of the array.

· String readLine (): read a text line.

· boolean ready(): Determines whether the stream is ready to be read.

· void reset(): resets the stream to the latest mark.

· long skip (long n): skip characters.

FileWriter: Convenience class for writing character files, which can be used to write character streams.

Construction method:

·FileWriter(File file): Constructs a FileWriter object based on the given File object.

FileWriter(String filename): Constructs a FileWriter object based on the given filename.

BufferedWriter: Writes text to a character output stream, buffering individual characters, providing efficient writing of individual characters, arrays, and strings.

 

 

Reference link: https://www.cnblogs.com/lca1826/p/6427177.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325314559&siteId=291194637