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.

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");
	    }
	}
}

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.

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...

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.

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

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

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

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
This question has already been answered. Start a new discussion instead.