java - 如何使用 BufferedInputStream 读取法语字符

标签 java

我正在尝试从文件中读取一些法语字符,但如果字母包含 à é è,则会出现一些符号。 谁能指导我如何获取文件的实际特征。 这是我的主要方法

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());
        }
    }
}

引用:How to read UTF-8 encoded data from a file

关于java - 如何使用 BufferedInputStream 读取法语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785066/

相关文章:

java - 通过插入合并 2 个二叉搜索树

java - 如何使用 Apache Camel 路由从授权服务器获取访问 token ?

java - 基于 Jax Ws 的 Projekt : Error:package javax. jws 不存在;程序找不到符号

java - Elasticsearch - 分配分片

java - Elasticsearch 找不到 JAVA_HOME 或 Java,即使这两者都存在

java - 如何从另一个类设置 TextView (Android)

java - Java 是否将除以 2 的幂的除法优化为位移位?

Java Spring 从 java 类创建数据库表给出 BeanCreationException

java - 一段时间后停止线程

java - 当按下某个组合键时,JFrame 可以重新出现吗?