Encoding issues on Java 7 file names in OS X

落花浮王杯 提交于 2019-11-28 04:00:17

问题


I have the following code:

public static void main( String[] args ) {
    System.out.println(Locale.getDefault());
    File f = new File("/Users/johngoering/Documents");
    File[] fs = f.listFiles();
    for (File ff : fs) {
        System.out.println(ff.getName());
        System.out.println(ff.exists());
    }
}

In my Documents folder I have a file called "öß.pdf". Here is the output under Java 6:

 en_US
 (...)
 öß.pdf
 true
 (...)

But here is the output under Java 7:

 en_US
 (...)
 o����.pdf
 false
 (...)

Note especially that file.exists returns false for a file returned by listFiles!! What gives? Is there any way to fix this? This seems like quite the Java 7 bug...


回答1:


With some help from Oracle, we discovered a workaround: the environment variable LC_CTYPE was not set to UTF-8 within Eclipse (and when starting from a JNLP or wherever else). This explains why the code worked on the terminal, since the OS X terminal by default "sets the locale environment variables" (an option which can be turned off and then you get the same issue as above even in the terminal).

Setting this environment variable in the launcher worked around the problem.

I still consider this a bug for Java 7 because Java 6 still worked even WITHOUT this variable.



来源:https://stackoverflow.com/questions/12509532/encoding-issues-on-java-7-file-names-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!