| Author |
How to rename a file in JAVA ?
|
Vicky Mohan
Ranch Hand
Joined: Oct 14, 2004
Posts: 130
|
|
Hey All, I have a file that i process for some business data. Once i process the data, i need to rename the file and place it in a specific directory ( i create the directory during the process). I create a new directory ( if it does not exist) and also create a new temp file (empty file) File.createTempFile(fileName, fileFormat, dir) . Now, i am trying to rename the existing file to this new destination. somehow, this operation always fails and a value of false is returned. Any help wil be appreciated
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
Have you released any FileReader associated with the file?
|
 |
Vicky Mohan
Ranch Hand
Joined: Oct 14, 2004
Posts: 130
|
|
FileInputStream input = new FileInputStream("file"); BufferedReader fileReader = new BufferedReader(new InputStreamReader(input)); ///// PROCESSING ///////// File destFile = new File("newfilePath"); boolean flag = file.renameTo(destFile); DO you mean to say that i need to close the fileReader ??
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Originally posted by Raj Verma: DO you mean to say that i need to close the fileReader ??
On Windows, yes, indeed. Note, also, that renameTo() will work only within a single filesystem (disk). To "rename" a file across disks, you need to copy the contents and delete the old file. It's a good idea to try renameTo(), then do copy/delete as a backup.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: How to rename a file in JAVA ?
|
|
|