java - 如何在java中正确读取阿拉伯数据集?

标签 java text encoding utf-8 arabic

场景:我想读取采用 utf-8 编码的阿拉伯数据集。每行中的每个单词都用空格分隔。

<小时/>

问题:当我阅读每一行时,输出为:

??????? ?? ???? ?? ???

<小时/>

问题:如何读取文件并打印每一行? 欲了解更多信息,here是我的阿拉伯数据集,读取数据的部分源代码如下所示:

private ContextCountsImpl extractContextCounts(Map<Integer, String> phraseMap) throws IOException {
        Reader reader;
        reader = new InputStreamReader(new FileInputStream(inputFile), "utf-8");
        BufferedReader rdr = new BufferedReader(reader);
        while (rdr.ready()) {
            String line = rdr.readLine();
            System.out.println(line);
            List<String> phrases = splitLineInPhrases(line);
            //any process on this file
        }
}

最佳答案

我可以使用UTF-8阅读,你可以这样尝试吗?

public class ReadArabic {
    public static void main(String[] args) {
        try {
            String line;
            InputStream fileInputStream = new FileInputStream("arabic.txt");
            Reader reader = new InputStreamReader(fileInputStream, "UTF-8"); // leave charset out for default
            BufferedReader bufferedReader = new BufferedReader(reader);
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage()); // handle all exceptions
        }
    }
}

Output

关于java - 如何在java中正确读取阿拉伯数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684167/

相关文章:

c++ - 插入器和提取器读取/写入二进制数据与文本

python - 用python将西里尔字母写入mysql

带有西类牙字符的 Git 字符编码

java - 使用 AffineTransform 将 PDFbox 转换为 iText 坐标

java - permission.android.MANAGE_USB 在 Jellybean 中有效,但在 Lollipop OS 中无效

Python 从 CSV 创建字典并使用文件名作为键

python - 打印两个字符串之间没有换行符

encoding - Android 上的 FFMPEG x264 编码 - 前瞻错误

java - 尝试删除文件时出现 AccessControlException

java - JTable 不显示新插入的行/数据