Introduction to the Java-io process

Source: Internet
Author: User

I. Three ways to classify IO streams

1. Divided by Flow direction: input stream and output stream

2. Different data units by stream are divided into: byte stream and character stream

3. According to the function of the stream is divided into: node flow and processing flow

Ii. the four abstract classes of IO flow

1. Character Stream: Reader (read) writer (write)

2. Byte stream: InputStream (read data) OutputStream (write data)

Introduction to three or four large abstract classes


1.Reader: Abstract class for reading stream of characters. Subclasses must implement only read (char[], int, int) and close (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functionality.

Method Details:

public int read (Charbuffer target) throws IOException attempts to read characters into the specified character buffer. The buffer can be used as the repository for characters: the only change that is made is the result of the put operation. does not reverse or re-wrap the buffer.

public int read () throws IOException reads a single character. This method is blocked until there are available characters, an I/O error occurred, or the end of the stream has been reached. Subclasses that are used to support efficient single-character input should override this method.

public int read (char[] cbuf) throws IOException the characters into the array. This method is blocked until an input is available, an I/O error occurs, or the end of the stream has been reached.

public abstract int Read (char[] cbuf,int off,int len) throws IOException reads a character into a portion of the array. This method is blocked until an input is available, an I/O error occurs, or the end of the stream is reached.

Public long Skip (long N) throws IOException skips characters. This method is blocked until a character is available, an I/O error occurs, or the end of the stream has been reached.

public Boolean Ready () throws IOException determines whether to read this stream.

public boolean marksupported () determines whether this stream supports the mark () operation. The default implementation always returns FALSE. Subclasses should override this method.

public void mark (int readaheadlimit) throws the current position in the IOException tag stream. Subsequent calls to reset () will attempt to relocate the stream to this point. Not all character input streams support the mark () operation.

public void Reset () throws IOException resets the stream. If the stream is marked, try repositioning the stream at that mark. If the stream is marked, an attempt is made to reset the stream in a way that is appropriate for a particular stream, for example, by repositioning the stream to its starting point. Not all character input streams support the reset () operation, and some support reset () and do not support mark ().

public abstract void Close () throws IOException closes the stream. After the stream is closed, further calls to read (), Ready (), Mark (), or reset () will throw IOException. However, closing a previously closed stream is not valid.

2.Writer: Abstract class that writes a stream of characters. Subclasses must implement methods that have only write (char[], int, int), flush (), and Close (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functionality.

Method Details:

public void write (int c) throws IOException writes a single character. The characters to be written are contained in 16 lows of the given integer value, and 16 high are ignored. Subclasses that are used to support efficient single character output should override this method.

public void Write (char[] cbuf) throws IOException writes an array of characters.

public abstract void Write (char[] Cbuf,int off,int len) throws IOException writes a portion of the character array.

public void Write (String str) throws IOException writes a string.

public void Write (string str,int off,int len) throws IOException writes a portion of the string.

Public Writer Append (charsequence csq) throws IOException appends the specified character sequence to this Writer.

The Out.append (CSQ) Form of a method call has exactly the same behavior as the following call:Out.write (csq.tostring ()) may not append the entire sequence, depending on the toString specification for the character sequence csq. For example, the ToString method that calls a character buffer returns a subsequence whose contents depend on the position and limit of the buffer.

Public Writer Append (charsequence csq,int start,int end) throws IOException appends the subsequence of the specified character sequence to this Writer. Appendable. When CSQ is not NULL, a method call in the form of Out.append (CSQ, start, end) has exactly the same behavior as the following call:Out.write (csq.subsequence (Start, end). ToString () )

Public Writer Append (char c) throws IOException appends the specified character to this writer. The Out.append (c) Form of a method call has exactly the same behavior as the following call:Out.write (c)

public abstract void Flush () throws IOException refreshes this stream. If this stream has saved any characters of the various write () methods in the buffer, they are immediately written to the intended destination. Then, if the target is another character or byte stream, it is refreshed. Therefore, a flush () call refreshes all buffers in the Writer and OutputStream chains. If the intended target of this stream is an abstraction (such as a file) provided by the underlying operating system, flushing this stream can only guarantee that bytes that were previously written to the stream are passed to the operating system for writing, but these bytes are not guaranteed to actually be written to a physical device, such as a disk drive.

public abstract void Close () throws IOException closes the stream, but refreshes it first. After the stream is closed, calling write () or flush () will cause the IOException to be thrown. However, closing a previously closed stream is not valid.

3.InputStream: This abstract class is a superclass of all classes that represent byte input streams.

Method Details:

public abstract int Read () throws IOException reads the next data byte from the input stream. Returns an int byte value in the range 0 to 255. Returns a value of 1 if no bytes are available because the end of the stream has been reached. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown. Subclasses must provide an implementation of this method.

public int read (byte[] b) throws IOException reads a certain number of bytes from the input stream and stores it in buffer array B. Returns the number of bytes actually read in integer form. This method is blocked until the input data is available, the end of the file is detected, or an exception is thrown.

If B is null, NullPointerException is thrown. If the length of B is 0, no byte is readable and 0 is returned, otherwise an attempt to read at least one byte is attempted. If no bytes are available because the stream is at the end of the file, a value of 1 is returned, or at least one byte can be read and stored in B. The first byte to be read is stored in the element b[0], the next one is stored in b[1], and so on. The number of bytes read is at most equal to the length of B. Let K be the number of bytes actually read, which will be stored between elements b[0] to b[k-1], without affecting the element b[k] to b[b.length-1]. throws IOException If the first byte is not read because the stream is not at the end of the file. In particular, if the input stream is closed, IOException is thrown. The effect of the read (b) method of class InputStream is equivalent to: Read (b, 0, B.length)

Public int Read (byte[] b,int off,int len) throws IOException reads up to Len data bytes in the input stream into a byte array. Attempts to read up to Len bytes, but may read fewer quantities. Returns the number of bytes actually read in integer form. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown. if off is negative, or Len is negative, or off+len is greater than the length of array B, indexoutofboundsexception is thrown. If Len is 0, no bytes are readable and 0 is returned, otherwise an attempt to read at least one byte is attempted. If no bytes are available because the stream is at the end of the file, a value of 1 is returned, or at least one byte can be read and stored in B. The first byte to be read is stored in the element B[off], the next one is stored in b[off+1], and so on. The number of bytes read is at most equal to Len. Let K be the number of bytes actually read; The bytes will be stored between elements B[off] through b[off+k-1], and the remaining elements b[off+k] to b[off+len-1] will not be affected. In any case, elements b[0] through B[off] and elements B[off+len] to b[b.length-1] will not be affected. If the first byte cannot be read because the stream is at the end of the file, IOException is thrown. In particular, if the input stream is closed, IOException is thrown. The read (b, off, Len) method of the class InputStream only repeats the method called Read (). If the first such call results in a IOException, the exception is returned from the call to the read (b, off, Len) method. If any subsequent call to read () causes IOException, the exception is caught and the location where the exception occurs is treated as the end of the file, the bytes read at that point are stored in B and the number of bytes read before the exception occurs. It is recommended that subclasses provide a more efficient implementation of this method.

Public long Skip (long N) throws IOException skips and discards n bytes of data in this input stream. For various reasons, the Skip method skips some small number of bytes (possibly 0) after the end. This can be caused by a variety of conditions, and the end of a file before skipping n bytes is just one possibility. Returns the actual number of bytes skipped. If n is negative, no bytes are skipped. The Skip method of the InputStream creates a byte array and then repeats it to the read-in until it reads n bytes or reaches the end of the stream. It is recommended that subclasses provide a more efficient implementation of this method.

public int available () throws IOException returns the number of bytes that the next caller of this input stream method can read (or skip) from this input stream in a blocked manner. The next caller may be the same thread or another thread. The available method of the class InputStream always returns 0. This method should be overridden by subclasses.

public void Close () throws IOException closes this input stream and frees all system resources associated with the stream. The Close method of the InputStream does not take any action.

public void mark (int readlimit) marks the current position in this input stream. Subsequent calls to the Reset method reposition the stream at the last mark, so that subsequent reads re-read the same bytes.
The Readlimit parameter tells the input stream to allow many bytes to be read before the mark position expires. Mark's general agreement is that if the method marksupported returns true, the input stream will always remember all bytes read after calling Mark, and whenever the method reset is called, it is ready to supply those same bytes again. However, if you can read more than Readlimit bytes from the stream before you call reset, you do not need the stream to remember any data at all.

public void Reset () throws IOException this stream to the location when the mark method was last called on this input stream.
The general contract for Reset is: If the method marksupported returns true:

IOException may be thrown if the method mark has not been called since the stream was created, or the number of bytes read from the stream since the last call to mark is greater than the last parameter that was called by Mark.
If such a ioexception is not thrown, the stream is reset to this state: all bytes read since the last call to mark (or, if Mark is not called) will be re-supplied to subsequent callers of the Read method, followed by a call to reset All bytes of the next input data.
If the method marksupported returns false, then:
The call to reset may throw IOException.
If IOException is not thrown, the stream is reset to a fixed state that depends on the specific type of the input stream and the fixed state of how it was created. The bytes provided to subsequent callers of the Read method depend on the specific type of input stream.
In addition to throwing IOException, the class InputStream method of reset does nothing.

The InputStream's Mark method performs no action.

public boolean marksupported () tests whether this input stream supports the mark and reset methods. Whether Mark and reset are supported are invariant properties for a particular input stream instance. The marksupported method of InputStream returns false.

4.OutputStream: This abstract class is a superclass of all classes that represent output byte streams. The output stream accepts output bytes and sends those bytes to a sink. Applications that need to define OutputStream subclasses must always provide at least one method that can write an output byte.

Method Details:

public abstract void Write (int b) throws IOException writes the specified bytes to this output stream. The general contract for write is: writes a byte to the output stream. The byte to write is the eight low of the parameter B. The 24 highs of B will be ignored. Subclasses of OutputStream must provide an implementation of this method.

public void Write (byte[] b) throws IOException writes B.length bytes from a specified byte array to this output stream. The general contract for write (b) is that the effect should be exactly the same as calling write (b, 0, b.length).

public void Write (byte[] B,int off,int len) throws IOException writes the specified byte array of len bytes starting at offset off to this output stream. The general contract for write (b, off, Len) is to write some bytes in array b sequentially to the output stream; element B[off] is the first byte written by this operation, and B[off+len-1] is the last byte written by this operation. The Write method of the OutputStream invokes the write method of a parameter on each byte to be written out. It is recommended that subclasses override this method and provide a more efficient implementation. if B is null, NullPointerException is thrown. if off is negative, or Len is negative, or off+len is greater than the length of array B, indexoutofboundsexception is thrown.

public void Flush () throws IOException refreshes this output stream and forces all buffered output bytes to be written out. The general agreement for Flush is that if the implementation of this output stream has buffered any previously written bytes, calling this method indicates that the bytes should be written to their intended target immediately. If the intended target of this stream is an abstraction (such as a file) provided by the underlying operating system, flushing this stream can only guarantee that bytes that were previously written to the stream are passed to the operating system for writing, but these bytes are not guaranteed to actually be written to a physical device, such as a disk drive. the Flush method of the OutputStream does not perform any action.

public void Close () throws IOException closes this output stream and frees all system resources related to this stream. The general contract for Close is: The method will close the output stream. A closed stream cannot perform an output operation, nor can it be reopened. The Close method of the OutputStream does not take any action.

Introduction to the Java-io process

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.