Reliable File.Renameto() Alternative on Windows

Reliable File.renameTo() alternative on Windows?

See also the Files.move() method in JDK 7.

An example:

String fileName = "MyFile.txt";

try {
Files.move(new File(fileName).toPath(), new File(fileName).toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
Logger.getLogger(SomeClass.class.getName()).log(Level.SEVERE, null, ex);
}

Renaming a file without using renameTo() - Java

A rename would rename it... if it were on the same filesystem.

If a renameTo() fails, you'll need to copy it to the new location, then delete the original.

File.renameTo() doesn't have any effect

For future browsers: This was fixed with Assylias' comment. Below you will find the eventual code which fixed it.

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(" ", " ");
Path source = currentFile.toPath();
try {
Files.move(source, source.resolveSibling(formattedName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


Related Topics



Leave a reply



Submit