IO之转换流与重定向标准输入/输出

来源:互联网 发布:淘宝云标签考试 编辑:程序博客网 时间:2024/05/18 01:45


Java的IO体系还提供了两个转换流,用于实现将字节流转换成字符流。其中InputStreamReader将字节输入流转换成字符输入流,OutputStreamWriter将字节输出流转换成字符输出流。
转换流有两个作用:
1)可以将字节流转换成字符流
2)可以使用指定的编码表

1.将字节流转换成字符流
Java使用System.in代表标准输入,即键盘输入。但这个标准输入流是InputStream类的实例,使用不方便。键盘输入的内容是文本内容,所以可以使用InputStreamReader将其转换成字符输入流。普通的Reader读取输入内容时依然不太方便,可以将普通的Reader再次包装成BufferedReader,利用BufferedReader的readLine方法进行一行一行读取内容。
public class KeyIn {public static void main(String[] args) {//定义带缓冲的字符流BufferedReader bufr = null;try{//使用转换流将字节流转换成字符流并包装bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;//使用循环的方式一行一行读while((line = bufr.readLine()) != null){//如果读取的字符串是"over",那么停止读取if("over".equals(line))break;System.out.println("输入内容是:" + line);}}catch(IOException e){e.printStackTrace();}finally{try{if(bufr != null)bufr.close();}catch(IOException e){e.printStackTrace();}}}}


2.使用指定编码表
转换流的另一个作用是可以使用指定的编码表。
InputStreamReader/OutputStreamWriter和FileReader/FileWriter是同级的。区别是FileReader/FileWriter使用的是平台默认的编码表并且不可以更改。
使用InputStreamReader/OutputStreamWriter的构造器可以更改编码表:

InputStreamReader(InputStream in, String charsetName):创建使用指定字符集的 InputStreamReader。
OutputStreamWriter(OutputStream out, String charsetName):创建使用指定字符集的 OutputStreamWriter。

FileReader和FileWriter使用默认平台的编码表,不可以更改。
/* * 使用FileReader和FileWriter拷贝文本文件 * */public class CopyText {public static void main(String[] args) {//FileReader和FileWriter使用平台默认编码表,不可以更改FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("c:\\demo.txt");fw = new FileWriter("d:\\demo.txt");char[] buf = new char[1024];int len;while((len = fr.read(buf)) != -1){fw.write(buf, 0, len);fw.flush();}}catch(IOException e){e.printStackTrace();}finally{try{if(fr != null)fr.close();}catch(IOException e){e.printStackTrace();}try{if(fw != null)fw.close();}catch(IOException e){e.printStackTrace();}}}}

InputStreamReader/OutputStreamWriter可以指定编码表
/* * 使用不同的编码表将键盘录入信息保存到文本文件 * */public class WriteText {public static void main(String[] args) {//用于读取键盘录入BufferedReader bufr = null;//用于将内容输出到文本文件BufferedWriter bufw1 = null;BufferedWriter bufw2 = null;try{//包装读取转换流bufr = new BufferedReader(new InputStreamReader(System.in));//包装FileWriter使用平台默认编码表bufw1 = new BufferedWriter(new FileWriter("d:\\demo1.txt"));//包装转换流OutputStreamWriter并指定与平台不同的编码表bufw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\demo2.txt"), "utf-8"));String line = null;while((line = bufr.readLine()) != null){bufw1.write(line);bufw1.flush();bufw2.write(line);bufw2.flush();}}catch(IOException e){e.printStackTrace();}finally{try{if(bufr != null)bufr.close();}catch(IOException e){e.printStackTrace();}try{if(bufw1 != null)bufw1.close();}catch(IOException e){e.printStackTrace();}try{if(bufw2 != null)bufw2.close();}catch(IOException e){e.printStackTrace();}}}}

在键盘录入“你好”,看到保存的demo1大小是4字节,而demo2大小是6字节。
如果使用FileReader来读取两个文件,看到读取demo2会出现乱码。因为FileReader使用平台默认的GBK编码表,而demo2使用的是UTF-8编码表。


3.标准输入输出重定向
Java的标准输入和输出分别通过System.in和System.out来代表,默认情况下它们分别代表键盘和显示器。
在System类里提供了重定向标准输入和输出的方法:

static void setIn(InputStream in):重定向标准输入流
static void setIn(PrintStream out):重定向标准输出流

/* * 重定向标准输出为文件输出 * */public class SetIn {public static void main(String[] args) {PrintStream ps = null;try{ps = new PrintStream(new FileOutputStream("out.txt"));System.setOut(ps);System.out.println("hello java");}catch(IOException e){e.printStackTrace();}finally{if(ps != null)ps.close();}}}



0 0