所有位于java.io包内的stream类型分别都继承自一下四种抽象stream类型:
字节 Bytes Stream | 字符 Character Stream | |
---|---|---|
Input Stream | InputStream | Reader |
Output Stream | OutputStream | Writer |
InputStream
InputSteam的基本方法:
Modifier and Type | Method and Description |
---|---|
int | available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
|
void | close()
Closes this input stream and releases any system resources associated with the stream.
|
void | mark(int readlimit)
Marks the current position in this input stream.
|
boolean | markSupported()
Tests if this input stream supports the mark and reset methods.
|
abstract int | read()
Reads the next byte of data from the input stream.
|
int | read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b .
|
int | read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes.
|
void | reset()
Repositions this stream to the position at the time the mark method was last called on this input stream.
|
long | skip(long n)
Skips over and discards n bytes of data from this input stream.
|
灰色的代表节点流。白色的代表处理流。
FileInputStream
从一个文件中获取字节数据;通常用于读取原始的字节数据文件,如image data;对与字符型文件,推荐采用FileReader。
PipedInputStream
实现了pipe的概念,主要在线程中使用。管道输入流是指一个通讯管道的接收端。
一个线程通过管道输出流发送数据,而另一个线程通过管道输入流读取数据,这样可以实现两个线程间的通讯:
BypteArrayInputStream
StringBufferInputStream
不推荐使用StringBufferInputStream,此类不能将字符正确的转换为字节。
从字符串创建流的最佳方法是采用StringReader。
SequenceInputStream
把多个InputStream合并成为一个InputStream,并使它们像单个输入流一样。每个输入流依次被读取,直到到达该流的末尾,然后关闭这个流并自动切换到下一个输入流。
OutputStream
OutputStream的基本方法:
Modifier and Type | Method and Description |
---|---|
void | close()
Closes this output stream and releases any system resources associated with this stream.
|
void | flush()
Flushes this output stream and forces any buffered output bytes to be written out.
|
void | write(byte[] b)
Writes b.length bytes from the specified byte array to this output stream.
|
void | write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.
|
abstract void | write(int b)
Writes the specified byte to this output stream.
|
灰色的代表节点流。白色的代表处理流。
FileOutputStream
像File或FileDescriptor输出数据。
PipedOutputStream
管道输出流是指一个通讯管道的发送端。一个线程通过管道输出流发送数据,另一个线程通过管道输入流读取数据,实现两个线程的通讯。
参见PipedInputStream
ByteArrayOutputStream
Reader
Reader的基本方法如下:
Modifier and Type | Method and Description |
---|---|
abstract void | close()
Closes the stream and releases any system resources associated with it.
|
void | mark(int readAheadLimit)
Marks the present position in the stream.
|
boolean | markSupported()
Tells whether this stream supports the mark() operation.
|
int | read()
Reads a single character.
|
int | read(char[] cbuf)
Reads characters into an array.
|
abstract int | read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.
|
int | read(CharBuffer target)
Attempts to read characters into the specified character buffer.
|
boolean | ready()
Tells whether this stream is ready to be read.
|
void | reset()
Resets the stream.
|
long | skip(long n)
Skips characters.
|
灰色的代表节点流。白色的代表处理流。
CharArrayReader
与ByteArrayInputStream,实现一个可用作字符输入流的字符缓冲区。
StringReader
与StringBufferInputStream对应,其源为一个字符串的字符流。
FileReader
与FileInputStream对应。
PipedReader
与PipedInputStream对应。
Writer
Writer的基本方法如下:
Modifier and Type | Method and Description |
---|---|
Writer | append(char c)
Appends the specified character to this writer.
|
Writer | append(CharSequence csq)
Appends the specified character sequence to this writer.
|
Writer | append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
|
abstract void | close()
Closes the stream, flushing it first.
|
abstract void | flush()
Flushes the stream.
|
void | write(char[] cbuf)
Writes an array of characters.
|
abstract void | write(char[] cbuf, int off, int len)
Writes a portion of an array of characters.
|
void | write(int c)
Writes a single character.
|
void | write(String str)
Writes a string.
|
void | write(String str, int off, int len)
Writes a portion of a string.
|
灰色的代表节点流。白色的代表处理流。
CharArrayWriter
与ByteArrayOutputStream对应。
StringWriter
无与之相对应的以字节为导向的stream
FileWriter
与FileOutputStream对应
PipedWriter
与PipedOutputStream对应