使用java读取UTF-8编码文本文件 [英] reading text file with utf-8 encoding using java
本文介绍了使用java读取UTF-8编码文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用java与netbeans 7.2.1平台
我已经配置了java项目处理UTF-8
javaproject ==>右键单击==>属性==>源==> UTF-8
但仍然获取未知字符输出:
代码:
文件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(异常e)
{
System.out.println(e.getMessage());
}
}
}
您需要将UTF- 8在引号
I have problem in reading text file with utf-8 encoding I'm using java with netbeans 7.2.1 platform
I already configured the java project to handle UTF-8 javaproject==>right click==>properties==>source==>UTF-8
but still get the unknown character output: ����� �������� ���� �
the code:
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);
}
any other ideas?
thanks
解决方案
Use
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());
}
}
}
You need to put UTF-8 in quotes
这篇关于使用java读取UTF-8编码文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文