aspose file tools
The moose likes Java in General and the fly likes How to rename a file in JAVA ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to rename a file in JAVA ?" Watch "How to rename a file in JAVA ?" New topic
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
    
  13

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]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to rename a file in JAVA ?
 
Similar Threads
Doubt in K&B mock exam
Show dir and file structure exactly as it is
unable to delete or rename old file
renameTo method in io package...
locking a file from processing