例子程序:
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(); } } }