java - 无法在Java中读取阿拉伯文本文件

标签 java file text

我正在尝试使用 Java 读取阿拉伯文本,但扫描仪看不到任何元素,因此尽管 LineNumberReader 可以识别文本文件中的行,但读取不成功。

我在英文文本上尝试了相同的代码,效果很好。

我使用的是netbeans 7.0.1

这是我的代码:

public class ReadFile {
    private int number_of_words;
    private File f1;
    private String array[][],lines[];
    private Scanner scan1;

    public ReadFile(String sf1) throws FileNotFoundException
    {
        f1=new File(sf1);
        scan1=new Scanner(f1);

    }

    public String[][] getA()
    {
        return array;
    }

    public void read() throws IOException
    {
        int counter=0,i=0;

        LineNumberReader  lnr = new LineNumberReader(new FileReader(f1));
        lnr.skip(Long.MAX_VALUE);
        number_of_words=lnr.getLineNumber();
        array = new String[2][number_of_words];
        lines = new String[number_of_words];
        while(scan1.hasNext())
      {
        String temp;
        temp=scan1.nextLine();
        lines[counter++] = temp;
                        System.out.println(lines[counter-1]+"\t"+lines.length);

      }

       Arrays.sort(lines);
       counter=0;

       while(i<lines.length)
       {
           String temp = lines[i++];
           StringTokenizer tk=new StringTokenizer(temp,"\t");

           array[0][counter] = tk.nextToken();
           array[1][counter++] = tk.nextToken();
       }
     }
 } 

最佳答案

默认情况下,扫描仪使用系统编码。读取数据特殊字符时需要使用正确的字符编码。

scan1=new Scanner(f1, "UTF-8");

如果 UTF-8 不起作用,您需要尝试使用阿拉伯语特定编码。

这里有几个可能有用的链接 File reading practicesJava supported encodings

关于java - 无法在Java中读取阿拉伯文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215104/

相关文章:

java - 在Java中通过套接字传输文件的缓冲区大小应该是多少

java - 在java中使用哈希码比较两个大字符串?

java图像接收(网络)服务器

javascript - 如何在 JavaScript 中通过换行符从字符串创建数组?

java - 字符串的 ArrayList 作为参数并返回列表中的最后一项

java - 如何从 imageView 获取图像并将其存储到 firebase 存储

java计算器循环无法正常工作

java - 文件开头不需要的字符,通过 TCP 连接在 Android 和 PC 之间发送

java - Java 中的输出文件不断被覆盖

python - 如何使用 PYPDF2 从 pdf 中提取表值?