Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Move file to archive folder

807599Feb 8 2007 — edited Feb 8 2007
hi all,

I've really exhausted all my resources through searching the web for answers and looking up books, but I'm still unable to solve my problem. Please kindly advice me.

I'm writing a program that:
1) reads all the csv files that are in a folder
2) inserts the valid records in the files into my database
3) move the file to an archive folder

I have successfully completed steps 1 and 2. But I'm having lots of trouble with Step 3. Below is my codes:
// initialize the file source and target destination
String source = "E:\\csv\\new\\abc.csv";
String target = "E:\\csv\\archive\\abc.csv";

// using command prompt to move the file
// e.g. "cmd /c move E:\csv\new\abc.csv E:\csv\archive\abc.csv"
Runtime run  = Runtime.getRuntime();

String cmd = "cmd /c move ";			

cmd = cmd + source + " " + target;
		
Process p = run.exec( cmd ); 

// check if file has been successfully moved to target destination
File f = new File ( target );

if ( f.exists() )
    System.out.println("File has been moved successfully");
else
    System.out.println("File has NOT been moved successfully");
What happened is that my file has been moved successfully but f.exists() returns a false! I tried all sorts of ways like changing my forward slashes to backward slashes etc... but to no avail. Furthermore, the abc.csv file is only 2KB, so I think it would take a split of the second to move the file.

By the way, I've also tried using file.renameTo() method but my file does not even get moved. I've been searching the web for 2 days already and I'm really at a lost. Please help me! Thank you so much in advance! :o)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 8 2007
Added on Feb 8 2007
11 comments
733 views