解决读取文件乱码问题

首先要明白自己的文件是什么格式,需要设置为与源文件格式相同(本人就是在这里出现问题了)

以下是notepad++查看文件编码格式的方法。

代码,若源文件为UTF-8,则需要把GBK改为UTF-8!!!

FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;

try {
	String filePath = "E:\\aaaDEel\\fc\\test00000.txt";
	File file = new File(filePath);
	if (file.isFile() && file.exists()) {
		fis = new FileInputStream(file);
		isr = new InputStreamReader(fis, "GBK");// 需要与源文件编码格式相同
		br = new BufferedReader(isr);
		String lineTxt = null;
//      lineTxt = br.readLine();// 跳过第一行表头
		while ((lineTxt = br.readLine()) != null) {
			System.out.println(lineTxt);
		}
	}

} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}finally {
	if (br != null) {
		try {
			br.close();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	if (isr != null) {
		try {
			isr.close();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	if (fis != null) {
		try {
			fis.close();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
}

以上。

猜你喜欢

转载自blog.csdn.net/leyili_s/article/details/82414913
今日推荐