package com.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * 文件读取之中文乱码问题 * * @author 刘军 * */ public class TestTransForm { public static void main(String[] args) throws IOException { File f = new File("G:/root/a.txt"); FileOutputStream fos = new FileOutputStream(f, true); OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); BufferedWriter bw = new BufferedWriter(osw); // bw.write("大家好");// ISO8859_1并不支持中文 bw.write("Hello world 你好,世界"); System.out.println(osw.getEncoding());// 如果不设置,默认是本地的,也就是GBK bw.close(); FileInputStream fis = new FileInputStream(f); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); BufferedReader br = new BufferedReader(isr); String s = null; try { s = br.readLine(); while (s != null) { System.out.println(s.toUpperCase()); s = br.readLine(); } br.close(); } catch (Exception e) { e.printStackTrace(); } } }