Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Renaming not happening  RSS feed

 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Freinds,
I'm trying this piece of code to work.But some how the temp file which is not getting renamed to the name of the file which is being sent as a parameter to this method.Where am i going wrong in this code.
Lets say name of the file passed as parameter is "prashanth.java".

Thanks in adavnce

[ April 19, 2007: Message edited by: Joe Ess ]
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess to rename tempjava.java file to prashanth.java below should suffice:



Hope this helps. Also use File.separator instead of '\'
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashanth, always write the code inside the code tags, it makes the code readable.
Answering your question, this is what the javadoc for rename says:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

In your case, the file you are trying to rename to will always exist, so this rename will not be successful.
 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I used File.separator but it dint work.Infact "Prashanth.txt" was just an example.This method is called in a loop which will pass files dynamically.As and when files comes in this method should create a temp file and atlast that temp file will be renamed to the file which is send as a parameter to this method.

i'm not able to figure out where exactly the problem is

regards
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding two tests to your code:
First, make sure the File instance passed to writeTheFile does not exist. If it does, you will not be able to rename tempFile to the same name.
Second, examine the return code from tempFile.renameTo().
Let us know what you find.
 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Infact i'm passing a file reference which is been obtained from the other method.So that buffer was not closed in that method hence program was failing.Thanks friends for your valuable suggestions.
I closed the buffer and program is doing what is expected.

thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!