码迷,mamicode.com
首页 > 其他好文 > 详细

转换流

时间:2014-12-11 17:17:49      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   java   on   文件   

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();
            }
        }
    }
    
}
}

转换流

标签:style   blog   io   color   os   sp   java   on   文件   

原文地址:http://www.cnblogs.com/yjtm53/p/4157949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!