首页 > 代码库 > 转换流

转换流

1 编码:(程序)字符串 –>字节数组(文本文件) OutputStreamWriter

   解码: (文本文件)字节数组 –>字符串(程序)InputStreamReader

2 标准的输出流: System.out

   标准的输入流: System.in

package lianxi1;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import org.junit.Test;public class TestConvertStream {@Test  public void test1(){    BufferedReader br = null;    BufferedWriter bw = null;    try {        FileInputStream fis = new FileInputStream("Goodbye");        InputStreamReader isr = new InputStreamReader(fis,"ISO-8859-1");        br = new BufferedReader(isr);  // 参数是Reader接口,InputStreamReader是子类,接口的多态                FileOutputStream fos = new FileOutputStream("GoodMood");        OutputStreamWriter osr = new OutputStreamWriter(fos,"ISO-8859-1");        bw = new BufferedWriter(osr);        String str;        if((str=br.readLine())!=null){            bw.write(str);            bw.newLine();            bw.flush();                    }    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();    } finally{        if(bw!=null){            try {                bw.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        if(br!=null){            try {                br.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }    }}

转换流