This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Moving files in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Moving files in java" Watch "Moving files in java" New topic
Author

Moving files in java

Kevin Hamrick
Ranch Hand

Joined: Aug 30, 2004
Posts: 32
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

Joined: Aug 30, 2004
Posts: 32
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

Joined: Aug 30, 2004
Posts: 32
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving files in java
 
Similar Threads
File object can't find directory
creating directory
Development: trouble with import and classpath
Ant copy files by reading property file
classpath setup by taking jars from multiple directory