Mac上的JFileChooser看不到中文字符命名的文件? [英] JFileChooser on Mac cannot see files named by Chinese chars?
问题描述
在Intellij中运行时程序运行正常(可以看到中文命名文件)。
The program worked fine when running in Intellij (it can see the Chinese named files).
我将它构建成.jar文件。执行jar并且 JFileChooser
看不到这些文件。
I built it into a .jar file. Executed the jar and the JFileChooser
cannot see those files.
我在Windows中尝试了jar,它完全有效很好。
And I tried the jar in Windows, it works completely fine.
推荐答案
这适用于我在Mac OS X 10.8.2上运行的文件:
This works file for me on Mac OS X 10.8.2:
import java.io.File;
import javax.swing.JFileChooser;
public class JFileChooserTest
{
public static void main(String[] args)
{
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
String path;
if(args.length > 0)
path = args[0];
else
path = System.getProperty("user.dir", ".");
File dir = new File(path);
JFileChooser jfc = new JFileChooser(dir);
int result = jfc.showOpenDialog(null);
switch(result) {
case JFileChooser.CANCEL_OPTION:
System.out.println("User cancelled OPEN dialog.");
break;
case JFileChooser.APPROVE_OPTION:
System.out.println("User chose file: " + jfc.getSelectedFile());
break;
case JFileChooser.ERROR_OPTION:
System.out.println("User encountered an error");
break;
default:
System.out.println("Confused");
break;
}
System.exit(0);
}
}
这是一个示例运行:
$ java -showversion JFileChooserTest
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
file.encoding=UTF-8
User chose file: /.../测试文件.txt
这是另一个样本运行:
$ java -showversion -Dfile.encoding=ISO-8859-1 JFileChooserTest
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
file.encoding=ISO-8859-1
User chose file: /.../????.txt
In在这两种情况下,文件选择对话框都正确显示了文件的名称(测试文件.txt)。
In both cases, the file-selection dialog properly displayed the name of the file (测试文件.txt).
注意使用 java.awt。 FileDialog
将为您提供特定于平台的文件对话框大多数Mac OS用户习惯于看到。虽然它不是严格的Swing(并且具有极小的功能集),但对于诸如OPEN和SAVE对话框之类的东西,它可能优于 JFileChooser
。 (它还显示中文字符在我的系统上没有问题。)
Note that using java.awt.FileDialog
will get you the platform-specific file dialog that most Mac OS users are used to seeing. Though it's not strictly Swing (and has an abysmally small feature set), it is probably superior to JFileChooser
for things like OPEN and SAVE dialogs. (It also shows Chinese characters without a problem on my system).
这篇关于Mac上的JFileChooser看不到中文字符命名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!