2012-02-09 57 views
2

我想使用Java讀取阿拉伯文本,但掃描儀沒有看到任何元素,因此雖然LineNumberReader識別文本文件中的行,但閱讀失敗。無法讀取Java中的阿拉伯語文本文件

我已經嘗試了相同的代碼在英文文本上,它工作正常。

我使用的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(); 
     } 
    } 
} 
+2

NetBeans是這裏無關緊要,所以我刪除該標籤。另外,接受更多答案 - 您的費率相當低。 – Paul 2012-02-09 16:58:09

回答

1

嘗試讀取這個文件:

FileInputStream fis = new FileInputStream(f1); 
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fis, "UTF-8")); 

您需要使用正確的Charset讀取時文件。

3

默認情況下,掃描儀使用系統編碼。讀取數據特殊字符時需要使用正確的字符編碼。

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

如果UTF-8無法正常工作,您需要嘗試使用阿拉伯語的特定編碼。

下面是幾個環節可能是有用的File reading practicesJava supported encodings

1

這很可能是你在找什麼:

Scanner(System.in, "UTF-8") 
相關問題