2012-11-02 26 views
1

我在SVN的存儲庫中有文件夾,其名稱中包含一個en-dash(「\ u2013」​​) 。 我首先調用「svn list」(以我的Windows 7 + UTF-8編碼)來獲取目錄列表。 在調用BufferedReader readLine()之後,它讀取列表的文本。 正在顯示的文件夾的名稱包含連字符(「\ u002D」)而不是連字符(「\ u2013」​​)。BufferedReader方法ReadLine()將en-dash(「 u2013」​​)轉換爲連字符(「 u002D」)

對此有任何限制嗎?

class Test { 
    public static void main(String args[]) { 
     BufferedReader br = null; 
     try { 
      String sCurrentLine; 
      br = new BufferedReader(new FileReader("C:\\test–ing.xml")); 
      System.out.println(br.readLine()); 
      while ((sCurrentLine = br.readLine()) != null) { 
       System.out.println(sCurrentLine); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null) 
        br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } // end main 
+1

您可能想要發佈一些代碼。我不清楚svn命令和Java程序是如何協同工作以及如何創建'BufferedReader'實例的。 – Codo

+0

你可以試試這個: 類測試{ \t公共靜態無效的主要(字符串ARGS []){ \t \t的BufferedReader BR = NULL; \t \t嘗試{ \t \t \t字符串sCurrentLine; \t \t \t br = new BufferedReader(new FileReader(「C:\\ test-ing.xml」)); \t \t \t System.out.println(br.readLine()); \t \t \t而((sCurrentLine = br.readLine())!= NULL){ \t \t \t \t的System.out.println(sCurrentLine); \t \t \t} \t \t}趕上(IOException的發送){ \t \t \t e.printStackTrace(); \t \t} {終於 \t \t \t嘗試{ \t \t \t \t如果(BR!= NULL) \t \t \t \t \t BR。關(); \t \t \t}趕上(IOException異常前){ \t \t \t \t ex.printStackTrace(); \t \t \t} \t \t} \t} //結束主 –

+0

我加你的代碼的問題。但我仍然不明白它與svn命令的關係。 – Codo

回答

1

這可能是這個問題:

br = new BufferedReader(new FileReader("C:\\test–ing.xml")); 

將使用平臺默認的編碼。你說,該文件是UTF-8編碼 - 所以你需要指定要UTF-8,這意味着避免FileReader的破API:

br = new BufferedReader(new InputStreamReader(
      new FileInputStream("C:\\test–ing.xml"), "UTF-8")); 

這是假設該文件確實包含預期字符的有效UTF-8。在做其他事之前,你應該檢查一下。

另外,鑑於這是XML,我假設在您的真實代碼中您將使用它作爲 XML?如果是這樣,我會直接從輸入流中加載它,並讓XML解析器處理編碼。

+0

您好Jon Skeet, 我在我的java程序中使用UTF-8編碼。我的操作系統是Windows 7. 問題沒有得到解決。 –

相關問題