user616639 user616639 - 5 months ago 10
Java Question

delete and rename a file in java

i've created a file "file1" in java and i read that "file1" and made some changes to the data read from "file1" and i wrote the new data to another file "file2"...now what i need is to delete the previous file "file1" and change the name of the file "file2" to "file1"...
please somebody help me with this....

Answer
//rename file
File file = new File("oldname");
File file2 = new File("newname");
boolean success = file.renameTo(file2);

//delete file
File f = new File("fileToDelete");
boolean success = f.delete();
Comments