我在读取 utf-8 编码的文本文件时遇到问题 我在 netbeans 7.2.1 平台上使用 java
我已经将 java 项目配置为处理 UTF-8 javaproject==>右键==>properties==>source==>UTF-8
但仍然得到未知字符输出: ��������������������
代码:
File fileDirs = new File("C:\\file.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(fileDirs), "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
还有其他想法吗?
谢谢
最佳答案
使用
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class test {
public static void main(String[] args){
try {
File fileDir = new File("PATH_TO_FILE");
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(fileDir), "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
您需要将 UTF-8 放在引号中
关于java - 使用java读取具有utf-8编码的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918188/