2012-11-02 27 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. 问题没有得到解决。 –

相关问题