本帖最后由 breeze 于 2013-4-24 19:56 编辑
这段代码指定了输入流和输出流的编码都是utf-8, 为什么到文件里却是乱码
程序是控制台循环接收一行字符数据, 当数据是quit时结束程序
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
try (InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("utff.txt"), "UTF-8"));
) {
System.out.println("输入字符");
String s;
while (!"quit".equals(s = br.readLine())) {
bw.write(s);
bw.newLine();
}
}
}
} |