Reputation: 47124
I'm trying to use the renameTo method in Java but it just returns false and doesn't move the file.
Am I missing a step? Is there a way to find out why it's not moving the file? The delete method doesn't do anything either.
Here's my code showing how I'm using it:
private void archiveOutputFile(File outputFile) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS");
String timeStamp = formatter.format(new Date());
String subFolderName = "Archive" + timeStamp;
File subFolder = new File(outputFile.getParent(),subFolderName);
subFolder.mkdir();
File newFile = new File(subFolder,outputFile.getName());
//outputFile.deleteOnExit(); //Doesn't work, nor does .delete()
boolean success = outputFile.renameTo(newFile);
}
Here's some system info:
Java: 1.6.0_21; Java HotSpot(TM) Client VM 17.0-b17
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)
Upvotes: 2
Views: 4207
Reputation: 32831
You need to create the subfolder before you move the file into it (uncomment subFolder.mkdir();)
Upvotes: 1
Reputation: 18741
I would recommend you to check for exists. public boolean exists()
Upvotes: 0
Reputation: 75376
You cannot rename or delete a file which Windows consider to be open.
Upvotes: 5