InputStreamReader
InputStreamReader和OutputStreamWriter类用于实现字符和字节之间的转化。 写入的OutputStreamWriter的字符用指定的编码方案翻译为字节,同样,从InputStreamReader类读出的字符用指定的编码方案解码为字符。
要指定文本文件的编码方式,使用InputStreamReader和OutputStreamWriter。
InputStreamReader和OutputStreamWriter不是节点流,是处理流,所以需要通过其他节点流链接到具体的数据源。
源码剖析:
继承关系、构造方法:
class InputStreamReader extends Reader
//创建一个使用默认字符集的 InputStreamReader
public InputStreamReader(InputStream in) {
super(in);
try {
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
} catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
}
// 创建使用指定字符集的 InputStreamReader
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
//创建使用给定字符集的 InputStreamReader
public InputStreamReader(InputStream in, Charset cs) {
super(in);
if (cs == null)
throw new NullPointerException("charset");
sd = StreamDecoder.forInputStreamReader(in, this, cs);
}
//创建使用给定字符集解码器的 InputStreamReader
public InputStreamReader(InputStream in, CharsetDecoder dec) {
super(in);
if (dec == null)
throw new NullPointerException("charset decoder");
sd = StreamDecoder.forInputStreamReader(in, this, dec);
}
方法介绍:
String getEncoding() :返回此流使用的字符编码的名称
int read():读取单个字符并返回,如果已达到文件末尾,返回-1
int read(char cbuf[], int offset, int length):将字符读入数组中的某一部分,最多length长度的字符到数组cbuf[],返回读取的字符数,如果已经达到文件末尾,则返回-1
boolean ready():判断此流是否已经准备好用于读取
void close():关闭流
OutputStreamWriter
class OutputStreamWriter extends Writer
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException("charset");
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException("charset encoder");
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}
方法介绍:
void flushBuffer()
void write(int c)
void write(char cbuf[], int off, int len)
void write(String str, int off, int len)
void flush()
void close()
写入/读取操作的具体步骤:
1.创建OutputStreamWriter/InputStreamReader对象(打开字符输出/输入流);
2.写入字符数据到字符输出流(自动编码)/从字符输入流读取字符数据(自动转换编码);
3.关闭流(关闭文件):写入完成后,应该使用close()关闭流,否则可能会导致缓冲的数据没有最终更新到文件中。
Demo:
public class InputStreamReaderDemo {
public static void main(String[] args) {
String path = "C:\\Users\\a\\Desktop\\L\\inputtest.txt";//123abc
try {
FileInputStream fileInputStream = new FileInputStream(path);
//字符输入转换流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
System.out.println(inputStreamReader.ready());//当前流是否已经打开
System.out.println(inputStreamReader.getEncoding());//返回当前使用的字符集编码
int b = 0;
while ((b = inputStreamReader.read()) != -1){
System.out.print(b + " ");//49 50 51 97 98 99
}
char[] chars ={'h','e','l','l','o'};
inputStreamReader.read(chars,0,5);
inputStreamReader.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}