java程序读取含中文的txt文件,会出现乱码。这是由于系统的编码格式和程序的编码不同。windows采用的编码格式是gbk,IDE默认的编码格式是utf-8。
package tao.test;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
String path = "e://aco.txt";
String path2 = "e://aco1.txt";
BufferedReader in = null;
BufferedWriter out = null;
String myResult = "";
in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "gbk"));
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path2), "gbk"));
String lineTxt = null;
while ((lineTxt = in.readLine()) != null) {
int prior = lineTxt.indexOf(":");
int end = lineTxt.length();
myResult = lineTxt.substring(prior + 1, end);
out.write(myResult + "\r\n");
}
in.close();
out.close(); //没有这句输出文件为空
}
}