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
Java Date() Giving the Wrong Date
What's the Difference Between Getpath(), Getabsolutepath(), and Getcanonicalpath() in Java
What Is the List of Valid @Suppresswarnings Warning Names in Java
Using Mockito with Multiple Calls to the Same Method with the Same Arguments
Add Context Path to Spring Boot Application
Print an Integer in Binary Format in Java
Ignore Fields from Java Object Dynamically While Sending as JSON from Spring MVC
How to Implement a Db Listener in Java
@Runwith(Mockitojunitrunner.Class) VS Mockitoannotations.Initmocks(This)
What Is the Purpose of Flush() in Java Streams
Java - Get a List of All Classes Loaded in the Jvm
Inetaddress.Getlocalhost() Throws Unknownhostexception
Do I Need to Close() Both Filereader and Bufferedreader
Does a Finally Block Always Run
Easy, Simple to Use Lru Cache in Java
Test If a String Contains Any of the Strings from an Array
Comparing Two Java.Util.Dates to See If They Are in the Same Day