在OS X中编码Java 7文件名的问题 [英] Encoding issues on Java 7 file names in OS X
问题描述
我有以下代码:
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());
}
}
在我的Documents文件夹中,我有一个名为öß .PDF。这是Java 6下的输出:
In my Documents folder I have a file called "öß.pdf". Here is the output under Java 6:
en_US
(...)
öß.pdf
true
(...)
但这里是Java 7下的输出:
But here is the output under Java 7:
en_US
(...)
o����.pdf
false
(...)
请特别注意 file.exists
返回 listFiles
返回的文件 false
!
什么给了?有没有什么办法解决这一问题?这似乎是相当的Java 7错误...
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...
推荐答案
有了Oracle的一些帮助,我们发现了一个解决方法:环境变量在Eclipse中(并从JNLP或其他任何地方开始)中,未设置 LC_CTYPE
未设置为 UTF-8
。
这解释了为什么代码在终端上工作,因为OS X终端默认情况下设置区域环境变量(一个可以关闭的选项,然后你会得到与上述相同的问题,即使在终端中)
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.
我仍然认为这是一个错误Java 7,因为Java 6仍然工作,没有这个变量。
I still consider this a bug for Java 7 because Java 6 still worked even WITHOUT this variable.
这篇关于在OS X中编码Java 7文件名的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!