943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1076
  • Java RSS
Feb 23rd, 2009
0

Moving Files

Expand Post »
Hi Folks,
I am having difficult with a program. I have a program that downloads a file to the directory it is being run from and I need to move it to another one after its done downloading.

I found code that should do it but it keeps failing, and driving me up the wall.

java Syntax (Toggle Plain Text)
  1. import java.io.File;
  2.  
  3. public class main{
  4. public static void main(String[] args){
  5. // File (or directory) to be moved
  6. File file = new File("test.txt");
  7.  
  8. // Destination directory
  9. File dir = new File("C:\\Program Files\\WOOT_IT_WORKED.txt");
  10.  
  11. // Move file to new directory
  12. boolean success = file.renameTo(new File(dir, file.getName()));
  13. if (!success) {
  14. System.out.println("Move Failed");
  15. }
  16. }
  17. }
Last edited by PhiberOptik; Feb 23rd, 2009 at 4:31 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Feb 23rd, 2009
0

Re: Moving Files

If the file you want to copy is already used by an another process (if this is it), you cannot have access to it (except if you have special permissions).

This is very useful program for spotting file handles for every process: Process Explorer. Google it if you have a chance.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Feb 23rd, 2009
0

Re: Moving Files

Okay, I checked out the Process Explorer program. Also, those files are just text files that I are not being used at all. I'm just testing the program right now...
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Feb 23rd, 2009
0

Re: Moving Files

On which OS are you using?. It may not do a difference, but the "slash-backslash" complexity can cause this.

Otherwise, look at the renameTo method. Look for special permissions or existence of the source file.
Last edited by GDICommander; Feb 23rd, 2009 at 4:49 pm.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Apr 18th, 2009
0

Re: Moving Files

This thread is marked "solved" but there is no solution posted. I will add that I believe it is the case that the issue lies with the location of the original file, which is not specified when the File object is created. Therefore, whether the move works or not will depend entirely on where the application expects the file to be when no absolute path is provided.

The code being used was probably picked up from one of a couple of places on the web that demonstrate this technique, and employ this same mistaken approach. Specify the absolute path to the original file and the code will work as expected. It's never a good idea to depend on the application's ability to figure out where the file is. Execution context will often make hash of your expectations.

Thanks.

mp
Reputation Points: 10
Solved Threads: 1
Newbie Poster
naugiedoggie is offline Offline
12 posts
since Apr 2009
Apr 19th, 2009
0

Re: Moving Files

Would you be kind enough to post some code that moves file the correct way?
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Apr 19th, 2009
0

Re: Moving Files

Would you be kind enough to post some code that moves file the correct way?
Hello,

Here you go. This example is extracted from an application that moves log files around. It works.

Thanks.

mp

Java Syntax (Toggle Plain Text)
  1.  
  2.  
  3. import java.io.File;
  4.  
  5. public class LogMover{
  6.  
  7. private String fileOrigin = null;
  8. private String fileDestination = null;
  9.  
  10.  
  11. public LogMover(String origin, String dest){
  12. this.setFileOrigin(origin);
  13. this.setFileDestination(dest);
  14. }
  15.  
  16. public static void main(String[] args) {
  17.  
  18. String origin = "\\\\ctpowem01\\origin\\";
  19. String dest = "\\\\ctpowem01\\dest\\";
  20.  
  21. LogMover lm = new LogMover(origin,dest);
  22. lm.moveFiles();
  23.  
  24. System.out.println("move completed.");
  25. } // end main
  26.  
  27. public void moveFiles(){
  28. boolean moved = false;
  29. File origin = new File(this.getFileOrigin());
  30.  
  31. File [] files = origin.listFiles();
  32.  
  33. for (int i=0; i<files.length; i++){
  34. File dir = new File(this.getFileDestination());
  35.  
  36. moved = files[i].renameTo(new File(dir,files[i].getName()));
  37. if(moved != true){
  38. System.out.println("file " + files[i].getName() + " not moved.");
  39. }
  40. }
  41. } // end moveFiles
  42.  
  43. public String getFileOrigin(){
  44. return fileOrigin;
  45. }
  46.  
  47. public void setFileOrigin(String newPath){
  48. fileOrigin = newPath;
  49. }
  50.  
  51. public String getFileDestination() {
  52. return fileDestination;
  53. }
  54.  
  55. public void setFileDestination(String fileDestination) {
  56. this.fileDestination = fileDestination;
  57. }
  58.  
  59. } // end class
Reputation Points: 10
Solved Threads: 1
Newbie Poster
naugiedoggie is offline Offline
12 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: ArrayIndexOutOfBoundsException: 1
Next Thread in Java Forum Timeline: Create & start threads with loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC