InputStreamReader(解码 1)/OutputStreamWriter(编码 2):是字节流和字符流相互转换的桥梁,以字符的形式操作字节流。
InputStreamReader
继承关系
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.
Each invocation of one of an InputStreamReader’s read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
For top efficiency, consider wrapping an InputStreamReader within a BufferedReader
Constructor Summary
Constructor | Description |
---|---|
InputStreamReader (InputStream in) | Creates an InputStreamReader that uses the default charset. |
InputStreamReader (InputStream in, String charsetName) | Creates an InputStreamReader that uses the named 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. |
Method Summary
Method Modifier and Type | Description |
---|---|
String getEncoding() | Returns the name of the character encoding being used by this stream. |
int read() | Reads a single character. |
int read(char[] cbuf, int offset, int length) | Reads characters into a portion of an array. |
boolean ready() | Tells whether this stream is ready to be read. |
OutputStreamWriter
继承关系
An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.
Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. Note that the characters passed to the write() methods are not buffered.
For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations.
Constructor Summary
Constructor | Description |
---|---|
OutputStreamWriter (OutputStream out) | Creates an OutputStreamWriter that uses the default character encoding. |
OutputStreamWriter (OutputStream out, String charsetName) | Creates an OutputStreamWriter that uses the named charset. |
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. |
Method Summary
Method Modifier and Type | Description |
---|---|
void flush() | Flushes the stream. |
String getEncoding() | Returns the name of the character encoding being used by this stream. |
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, int off, int len) | Writes a portion of a string. |
测试InputStreamReader读,OutputStreamWriter写
package cn.yzy.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
* InputStreamReader: 字节->字符
*/
public class convertTest01 {
public static void main(String[] args) {
/*
* InputStreamReader isr = new InputStreamReader(System.in);
* OutputStreamWriter osw = new OutputStreamWriter(System.out);
*/
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));){
/*
* 上述try部分使用了try-with-resource语法
* 注意:
* System.in和System.out处理的都是字符
*/
//循环获取键盘的输入(exit退出),并输出到控制台
String msgString = "";
while(!msgString.equals("exit")) {
msgString = reader.readLine();
writer.write(msgString);
writer.newLine();
writer.flush();
}
}catch(IOException e) {
System.out.println("操作异常");
}
}
}
以上try-with-resource语法请参考118.IO工具类文章末尾
测试网络字节流3指定字符集用InputStreamReader读和OutputStreamWriter写
package cn.yzy.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
/*
* 指定字符集
* 操作网络流(字节流)
* 下载百度源码
*/
public class convertTest02 {
public static void main(String[] args) {
// test01();
// test02();
// test03();
// test04();
test05();
}
static void test01() {
try(InputStream reader = new URL("http://www.baidu.com").openStream();){
int temp;
while((temp = reader.read()) != -1) {
System.out.print((char)temp); //乱码
}
}catch (Exception e) {
e.printStackTrace();
}
}
static void test02() {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream()));){
/*
* InputStreamReader网络字节流转换为字符流,并使用bufferedReader优化效率
*/
int temp;
while((temp = reader.read()) != -1) {
System.out.print((char)temp); //乱码,工程的字符集是GBK,而百度的源码字符集是UTF-8
}
}catch (Exception e) {
e.printStackTrace();
}
}
static void test03() {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(), "UTF-8"));){
/*
* 为了防止字符集不同导致的乱码问题,在InputStreamReader的第二个参数就要人为指定字符集
* 指定的字符集要随着输入的字符集而改变,才能保证不乱吗
*/
int temp;
while((temp = reader.read()) != -1) {
System.out.print((char)temp); //没有出现乱码
}
}catch (Exception e) {
e.printStackTrace();
}
}
static void test04() {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(), "UTF-8"));){
/*
* 改为按行读取,提升效率
*/
String tempString;
while((tempString = reader.readLine()) != null) {
System.out.print(tempString);
}
}catch (Exception e) {
e.printStackTrace();
}
}
static void test05() {
/*
* 测试输出流
*/
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
new URL("http://www.baidu.com").openStream(), "UTF-8"));
BufferedWriter writer = new BufferedWriter
(new OutputStreamWriter(
new FileOutputStream("baidu.html"), "UTF-8"));){
/*
* FileOutputStream是字节流
* FileOutputStream构造函数第二个参数不指定,默认就是和工程一致的
*/
String tempString;
while((tempString = reader.readLine()) != null) {
writer.write(tempString);
writer.newLine();
}
writer.flush();
}catch (Exception e) {
e.printStackTrace();
}
}
}