gpt4 book ai didi

java - 文件夹名称中的最后一个字符无法替换

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:49 25 4
gpt4 key购买 nike

我使用 .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]

最佳答案

您收到此异常是因为您的文件名中有一个空格作为最后一个字符,而底层操作系统 (Windows) 不接受带有尾随空格字符的文件名。

作为尾随字符的这个空格很可能是多个 String#replace 调用的结果,确保您没有用空格替换字符串中的最后一个字符。

关于java - 文件夹名称中的最后一个字符无法替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com