io流之转换流InputStreamReader、OutputStreamWriter

例子程序:

package io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TestTransform1 {

    public static void main(String[] args) {
        try {
            //转换流,字节流-->字符流
            OutputStreamWriter psw = new OutputStreamWriter(
                    new FileOutputStream("f:\\share\\j2se\\IO\\char.txt",true));//true表示追加到以前的内容后
            
            
            psw.write("北京市沙河,明天是端午节");//
            System.out.println(psw.getEncoding());//得到字符编码
            psw.flush();
            psw.close();
            
            //读取写入的数据
            InputStreamReader isr = new InputStreamReader(new FileInputStream("f:\\share\\j2se\\IO\\char.txt"));
            BufferedReader br = new BufferedReader(isr);
            String s = null;    
            s = br.readLine();
            System.out.println(s);
            
            br.close();
            //修改编码
            psw = new OutputStreamWriter(new FileOutputStream("f:\\share\\j2se\\IO\\char.txt"),"ISO8859_1");
        
            psw.write("aaabbbccc");
            System.out.println(psw.getEncoding());
            psw.flush();
            psw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

相关文章:

  • 2021-09-08
  • 2022-12-23
  • 2021-10-25
  • 2021-05-22
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
猜你喜欢
  • 2022-12-23
  • 2021-12-03
  • 2021-07-05
  • 2022-12-23
  • 2021-05-23
  • 2023-01-28
相关资源
相似解决方案