Take the 2-minute tour ×

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....

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted
//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();
share|improve this answer
add comment

You can use File.delete() and File.rename(File target) for this purpose.

See the Javadoc for java.io.File.

share|improve this answer
add comment

Basically, Java provides the needed API (See here for more):

share|improve this answer
add comment

Your Answer


