我使用 .replace() 指令来清理文件夹名称。到目前为止,这对这些字符效果很好:{“。”,“”,“(”,“[”}但是当我到达右括号时,我收到一个错误。当我查看抛出这个的文件夹时错误它总是在末尾有一个右括号。手动删除右括号并再次执行代码后,下一个带有尾括号的文件夹会发生错误。
在每种情况下,字符都被替换为一个空格。
public void cleanFormat() {
for (int i = 0; i < directories.size(); i++) {
File currentDirectory = directories.get(i);
for (File currentFile : currentDirectory.listFiles()) {
String formattedName = currentFile.getName();
formattedName = formattedName.replace(".", " ");
formattedName = formattedName.replace("(", " ");
formattedName = formattedName.replace(")", " "); // error here
formattedName = formattedName.replace("[", " ");
formattedName = formattedName.replace("]", " "); // and here
formattedName = formattedName.replace(" ", " ");
Path source = currentFile.toPath();
try {
Files.move(source, source.resolveSibling(formattedName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
JOptionPane.showMessageDialog(null, "All folders have been formatted");
}
错误是:
Exception in thread "AWT-EventQueue-0" java.nio.file.InvalidPathException: Trailing char < > at index 68: A Good Old Fashioned Orgy 2011 LIMITED 720p BluRay X264-AMIABLE EtHD
at sun.nio.fs.WindowsPathParser.normalize(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPath.parse(Unknown Source)
at sun.nio.fs.WindowsFileSystem.getPath(Unknown Source)
at sun.nio.fs.AbstractPath.resolveSibling(Unknown Source)
at domain.DirectoryListing.cleanFormat(DirectoryListing.java:86)
文件夹名称:
A Good Old Fashioned Orgy 2011 LIMITED 720p BluRay X264-AMIABLE EtHD]