2011-05-04 118 views
0

我試圖讀取一個ANSI使用以下兩種方式的Java讀取ANSI文件錯誤地

Scanner scanner = null; 
    try { 
      scanner = new Scanner(new File("test/input.txt"), "ISO-8859-6"); 

    while (scanner.hasNextLine()) { 
     String input =scanner.nextLine(); 
     processString(input); 
    } 

我想也默認編碼讀(即我省略了「ISO-8859-6編碼的阿拉伯語文件中的Java 「)

有什麼建議嗎?

+4

你怎麼驗證它是否正確讀取?我沒有看到任何暗示這一點的代碼。 – 2011-05-04 14:19:36

+0

@ JoachimSauer我在eclipse中使用了調試工具。當我用UTF-8文件測試它時,我可以看到阿拉伯文字符串正確顯示。 – Abdelwahed 2011-05-04 14:21:07

+1

@Abdelwahed,所以你有沒有試過把文件讀作「UTF-8」?你怎麼知道這個文件是用「ISO-8859-6」編碼寫的? – 2011-05-04 14:22:41

回答

0

試試這個代碼:

public static void transform(File source, String srcEncoding, File target,  String tgtEncoding) throws IOException { 
    BufferedReader br = null; 
    BufferedWriter bw = null; 
    try { 
     br = new BufferedReader(new InputStreamReader(new FileInputStream(source), Charset.forName(srcEncoding))); 
     bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), tgtEncoding)); 
     char[] buffer = new char[16384]; 
     int read; 
     while ((read = br.read(buffer)) != -1) { 
      bw.write(buffer, 0, read); 
     } 
    } finally { 
     try { 
      if (br != null) { 
       br.close(); 
      } 
     } finally { 
      if (bw != null) { 
       bw.close(); 
      }`enter code here` 
     } 
    } 
} 
0

看看這個:

 private static final String FILENAME = "/Users/jucepho/Desktop/ansi.txt"; 

    public static void main(String[] args) { 
      BufferedReader br = null; 
      FileReader fr = null; 
      try { 
       fr = new FileReader(FILENAME); 
       br = new BufferedReader(fr); 
       String sCurrentLine; 
       br = new BufferedReader(new FileReader(FILENAME)); 
       while ((sCurrentLine = br.readLine()) != null) { 
        System.out.println(sCurrentLine); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (br != null) 
         br.close(); 
        if (fr != null) 
         fr.close(); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 

這個文件有這個字http://www.alanwood.net/demos/ansi.html