Java - renameTo method not working
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 anyth开发者_开发知识库ing 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)
You cannot rename or delete a file which Windows consider to be open.
You need to create the subfolder before you move the file into it (uncomment subFolder.mkdir();)
I would recommend you to check for exists. public boolean exists()
More here
it works, if I comment in the line
subFolder.mkdir();
精彩评论