我正在尝试从文件中读取一些法语字符,但如果字母包含 à é è,则会出现一些符号。 谁能指导我如何获取文件的实际特征。 这是我的主要方法
public static void main(String args[]) throws IOException
{
char current,org;
//String strPath = "C:/Documents and Settings/tidh/Desktop/BB/hhItem01_2.txt";
String strPath = "C:/Documents and Settings/tidh/Desktop/hhItem01_1.txt";
InputStream fis;
fis = new BufferedInputStream(new FileInputStream(strPath));
while (fis.available() > 0) {
current= (char) fis.read(); // to read character
// from file
int ascii = (int) current; // to get ascii for the
// character
org = (char) (ascii);
System.out.println(org);
}
最佳答案
您正在尝试实际使用 ASCII 读取 UTF-8 字符。以下是如何实现您的功能的示例:
public class Test {
private static final FILE_PATH = "c:\\temp\\test.txt";
public static void main(String[] args){
try {
File fileDir = new File(FILE_PATH);
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));
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());
}
}
}
关于java - 如何使用 BufferedInputStream 读取法语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785066/