【发布时间】:2016-03-25 10:58:44
【问题描述】:
所以我正在制作一个 Java 应用程序/程序来备份特定文件夹(文件夹的名称很长,但它以“Pokémon”开头),我的问题是这个文件夹包含重音(这个 é)。我有一台 Mac,当我运行该程序时,它运行良好,但每当我在 Windows 上尝试它时,我的程序搜索一个文件夹,而不是“é”,我看到一个“e?” (在控制台中,当我打印路径的字符串时,我得到了这个 Poke?mon)。是 Windows 格式的问题吗?我该如何解决这个问题?
void SaveNow (String folderName) {
String fullOriginalPath = getMCPath() + "saves/Pokémon Cobalt and Amethyst [DEMO]";
String fullNewPath = getMCPath() + "PokeCA/" + folderName;
System.out.println("Path to original PokeCA map: " + fullOriginalPath); //At this point, the é is replaced by e?
System.out.println("Path to backup PokeCA map: " + fullNewPath);
if (OsUtils.isWindows()) {
fullOriginalPath = MakeWinPath(fullOriginalPath); //Replaces all / by \ because Windows
fullNewPath = MakeWinPath(fullNewPath); //Same
}
File source = new File(fullOriginalPath);
File dest = new File(fullNewPath);
try {
FileUtils.copyDirectory(source, dest);
System.out.println("Successfully backuped map!");
JOptionPane.showMessageDialog(frmSave, "Successfully backuped map!");
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frmSave, "Error, could not backup map... ;(");
}
注意:另外,我希望它适用于所有操作系统,这就是我使用 OsUtils.isWindows() 修改路径的原因。
谢谢!
【问题讨论】:
-
你能添加你的代码来获取这个文件夹吗?看起来您使用的 String 编码错误(例如使用 new String(utf8Bytes) 而不是 new String(utf8Bytes, "UTF8")) 。
-
可能是您的代码中的编码问题。
-
@ViacheslavVedenin 刚刚添加代码
-
“saves/Pokémon Cobalt and Amethyst [DEMO]”有问题吗?您可以尝试检查此 java 类文件的编码页面吗? UTF-8 与否?
-
@ViacheslavVedenin 我怎么知道它使用的是什么编码?,我应该将其更改为 new String("saves/...", "UTF-8") 吗?
标签: java io copy character directory