java – File. Renameto() has no effect
•
Java
I want to be able to rename the folder list to remove unnecessary characters (for example, dots and double spaces must become a single space)
When you click the button in the GUI, you will see a message box with a properly formatted name indicating that the function was properly formatted and called When I view the test folder I created, the name does not change (even after refresh) Using hard - coded strings doesn't work either
What am I overlooking?
public void cleanFormat() {
for (int i = 0; i < directories.size(); i++) {
File currentDirectory = directories.get(i);
for (File currentFile : currentDirectory.listFiles()) {
String formattedName = "";
formattedName = currentFile.getName().replace("."," ");
formattedName = formattedName.replace(" "," ");
currentFile.renameTo(new File(formattedName));
JOptionPane.showMessageDialog(null,formattedName);
}
}
}
Solution
For future browsers: This was fixed by comments from assylias You will find the final code to fix it below
public void cleanFormat() {
for (int i = 0; i < directories.size(); i++) {
File currentDirectory = directories.get(i);
for (File currentFile : currentDirectory.listFiles()) {
String formattedName = "";
formattedName = currentFile.getName().replace("."," ");
Path source = currentFile.toPath();
try {
Files.move(source,source.resolveSibling(formattedName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
