reading text file with utf-8 encoding using java
encoding, file-io, java, netbeans
Solution
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
Problem
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