上一篇,主要介绍的是InputStream和OutputStream的功能及其一些具体的实现类。
但是InputStream和OuputStream是都是基于byte来操作的。如果我们需要将Byte转换成Character,这个时候就会面临字符编码的问题。在java 1.0的时候,java只是将 8 bytes转换为 1 character,显然这种做法很不严谨。
因此在java 1.1,引入了Writer和Reader类,这两个类操作的基础是character。每次读取或者写入都是基于character为单元来实现的。并且StringBufferInputStream被弃用。
Reader类实现了Readable的接口。
int read(CharBuffer cb)
//Attempts to read characters into the specified character buffer.
Reader类的方法与InputStream很相似,所不同的是,Reader读取的单元是char.
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.
boolean ready()
//Tells whether this stream is ready to be read.
void reset()
//Resets the stream.
long skip(long n)
//Skips characters.
Reader类没有了InputStream的available()方法,但是多了一个ready()方法,ready()方法可以判断当前的Reader类是否可读。
java 1.0 InputStream与 java 1.1 Reader将如何兼容呢?
InputStreamReader很好的解决了这个问题。InputStreamReader可以将InputStream很好的转换成Reader。
InputStreamReader具有4个构造函数。
InputStreamReader(InputStream in)
//Creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream in, Charset cs)
//Creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec)
//Creates an InputStreamReader that uses the given charset decoder.
InputStreamReader(InputStream in, String charsetName)
//Creates an InputStreamReader that uses the named charset.
InputStreamReader还实现了方法获取当前的Encoding:
String getEncoding()
//Returns the name of the character encoding being used by this stream.
FileReader是InputStreamReader的子类,从文件中按字符读取数据。它使用了系统默认的编码。
StringReader取代了StringBufferInputStream的功能。它从String中读取字符。
CharArrayReader可以从Char数组中读取字符。
与BufferedInputStream一致,所不同的是BufferedReader内部通过Char数组来实现缓存的。
LineNumberReader继承了BufferedReader类,它可以记录当前读取数据所在的行,并且还支持按行读取数据。
int getLineNumber()
//Get the current line number.
String readLine()
//Read a line of text.
void setLineNumber(int lineNumber)
//Set the current line number.
FilterReader与FilterInputStream提供了相应的功能。
与PushbackStream提供了相同的功能。
Writer实现了Appendable接口,即Writer可以用Formatter交互。关于Formatter类的用法请看我的另一篇博客。
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.
与Reader很类似,OutputStreamWriter在OutputStream和Writer之间架了一座桥梁。
OutputStreamWriter提供了4个构造函数。
OutputStreamWriter(OutputStream out)
//Creates an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter(OutputStream out, Charset cs)
//Creates an OutputStreamWriter that uses the given charset.
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
//Creates an OutputStreamWriter that uses the given charset encoder.
OutputStreamWriter(OutputStream out, String charsetName)
//Creates an OutputStreamWriter that uses the named charset.
另外它也提供了getEncoding()方法。
String getEncoding()
//Returns the name of the character encoding being used by this stream.
FileWriter继承了OutputStreamWriter。它可以向文件写入数据,并且它使用的编码是系统默认的编码。
它是提供了将数据写入到String中的功能。它的内部是基于StringBuffer实现的。
另外它还提供了方法getBuffer()将数据转换为StringBuffer。
StringBuffer getBuffer()
//Return the string buffer itself.
CharArrayWriter可以向Char数组中写入数据。
FilterWriter与FilterOutputStream相似。
PrintWriter和PrintStream提供了相同的功能。并且它支持OutputStream作为参数,来构造PrintWriter。PrintWriter是直接继承Writer类的。
PrintWriter(File file)
//Creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter(File file, String csn)
//Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(OutputStream out)
//Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter(OutputStream out, boolean autoFlush)
//Creates a new PrintWriter from an existing OutputStream.
PrintWriter(String fileName)
//Creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter(String fileName, String csn)
//Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter(Writer out)
//Creates a new PrintWriter, without automatic line flushing.
PrintWriter(Writer out, boolean autoFlush)
//Creates a new PrintWriter.
PrintWriter的其他方法与PrintStream很相似。
这两个类的使用方法同PipedInputStream和PipedOutputStream相同。