Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving files in java

 
Kevin Hamrick
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is posted below. I created 2 files focusFile.txt and backUpFile.txt using file>>new>>etc.
I wrote into the files their file names and no other text.
When this method is called I get the following´┐Ż
--------------------FUNCTION CALLED-----------------------------
C:\dev\bin\eclipse\workspace\TEST\focusFile.txt
wrote to new file
alert('Focus File has been updated.');

Indicating to me that it worked. When I open the files again nothing has changed. What am I doing wrong?
Thanks for the insights´┐Ż
private void DeleteFocus()
throws java.io.IOException, java.sql.SQLException, java.lang.ClassNotFoundException{
{

System.out.println("--------------------FUNCTION CALLED-----------------------------");
String fileSep = System.getProperty("file.separator");
String root = "C:";
String dir1 = "dev";
String dir2 = "bin";
String dir3 = "eclipse";
String dir4 = "workspace";
String dir5 = "TEST";
String file1 = "focusFile.txt";
String file2 = "backUpFile.txt";
boolean success = false;
System.out.println(root + fileSep + dir1 + fileSep + dir2 + fileSep + dir3 + fileSep +
dir4 + fileSep + dir5 + fileSep + file1);
if( new File(root + fileSep + dir1 + fileSep + dir2 + fileSep + dir3 + fileSep +
dir4 + fileSep + dir5 + fileSep + file1).exists()) {
success = true;
try{
File file = new File(root + fileSep + dir1 + fileSep + dir2 + fileSep + dir3 + fileSep +
dir4 + fileSep + dir5 + fileSep + file1);
File dest = new File(root + fileSep + dir1 + fileSep + dir2 + fileSep + dir3 + fileSep +
dir4 + fileSep + dir5 + fileSep + file1);
file.renameTo(dest);
System.out.println("wrote to new file");
} catch(Exception e) {
System.out.println("BACKUP FAILED");

}


} else {

System.out.println("alert('BACKUP NONEXISTING');");

}
if(success){

System.out.println("alert('Focus File has been updated.');");

}



}

}//end DeleteFocus(javax.servlet.jsp.JspWriter out)
 
Kevin Hamrick
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update. On the dest file...I know it says file1 but I did change it to file2 before posting so that is not it.....
 
Kevin Hamrick
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI for those facing the same problem. You need to delete the destination file before doing anything else...i.e I added a method call at the begining which check to see if file2 existed and if so deleted it. I don't know if this is the best answer but it worked for me. If someone would like to add to this please do so.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you can clean this up a little by creating the "from" File object only once:

As I reformatted your code, I found an extra set of braces. I guess this was a copy-and-paste error. Although, if it is in your code, it might not cause a compiler error...hmmm.

And it is probably not a good idea to leave the try...catch clause in this code. Typically you want exceptions to propogate back as far as possible, imo. Of course, what you have here is good for debugging purposes but may not be "correct" for production code.

With that said, perhaps I can finally get around to answering your exact question. According to the Java API docs, File.renameTo() returns a boolean that indicates whether or not the rename operation succeeded. You should probably check this value rather than ignoring it as in your code above. Unfortunately, the javadocs do not describe what happens when the destination file already exists. Since your exploration shows that you need to delete the existing file, I suspect that renameTo() fails in such a situation.

Sorry for rambling on. I hope something here helps you, though.

Keep Coding!

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic