| | |
Moving Files
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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)
import java.io.File; public class main{ public static void main(String[] args){ // File (or directory) to be moved File file = new File("test.txt"); // Destination directory File dir = new File("C:\\Program Files\\WOOT_IT_WORKED.txt"); // Move file to new directory boolean success = file.renameTo(new File(dir, file.getName())); if (!success) { System.out.println("Move Failed"); } } }
Last edited by PhiberOptik; Feb 23rd, 2009 at 4:31 pm.
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
---------------------------------- Sir Winston Churchill
•
•
Join Date: Apr 2009
Posts: 8
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Apr 2009
Posts: 8
Reputation:
Solved Threads: 1
•
•
•
•
Would you be kind enough to post some code that moves file the correct way?
Here you go. This example is extracted from an application that moves log files around. It works.
Thanks.
mp
Java Syntax (Toggle Plain Text)
import java.io.File; public class LogMover{ private String fileOrigin = null; private String fileDestination = null; public LogMover(String origin, String dest){ this.setFileOrigin(origin); this.setFileDestination(dest); } public static void main(String[] args) { String origin = "\\\\ctpowem01\\origin\\"; String dest = "\\\\ctpowem01\\dest\\"; LogMover lm = new LogMover(origin,dest); lm.moveFiles(); System.out.println("move completed."); } // end main public void moveFiles(){ boolean moved = false; File origin = new File(this.getFileOrigin()); File [] files = origin.listFiles(); for (int i=0; i<files.length; i++){ File dir = new File(this.getFileDestination()); moved = files[i].renameTo(new File(dir,files[i].getName())); if(moved != true){ System.out.println("file " + files[i].getName() + " not moved."); } } } // end moveFiles public String getFileOrigin(){ return fileOrigin; } public void setFileOrigin(String newPath){ fileOrigin = newPath; } public String getFileDestination() { return fileDestination; } public void setFileDestination(String fileDestination) { this.fileDestination = fileDestination; } } // end class
![]() |
Similar Threads
- Moving files in c and c++ (C++)
- Moving Files with Dreamweaver (IT Professionals' Lounge)
- Files won't move or delete! (Storage)
- can't link to included files... y? (C++)
- Downloading music files to MP3 player (Windows 95 / 98 / Me)
- Annoying problem with moving files (Windows NT / 2000 / XP)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: ArrayIndexOutOfBoundsException: 1
- Next Thread: Create & start threads with loop
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows





