Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move a file from one location to another in Java?

Tags:

java

file

move

How do you move a file from one location to another? When I run my program any file created in that location automatically moves to the specified location. How do I know which file is moved?

like image 478
pmad Avatar asked Jan 10 '11 09:01

pmad


People also ask

How do I move a file from one directory to another in Java?

You can move a file or directory by using the move(Path, Path, CopyOption...) method. The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.

How do you move a file from one location to another?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.

How do I copy and paste a file in Java?

Another common way to copy a file with Java is by using the commons-io library. The latest version can be downloaded from Maven Central. Then, to copy a file we just need to use the copyFile() method defined in the FileUtils class. The method takes a source and a target file.

How do I transfer files from one FTP server to another in Java?

The proper steps to upload a file to FTP serverConnect and login to the server. Enter local passive mode for data connection. Set file type to be transferred to binary. Create an InputStream for the local file.

How to move a file in Java?

Now what we will do to move the file in Java is, provide the path in method parameter with the same name, which will result in moving the file from our original directory to our destination directory with the same name. Here is how our final Java code will look like.

How to move a file from one location to another?

The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source. Using Files.Path move () method: Renaming and moving the file permanently to a new location.

How do I get the path of a file in Java?

An instance of java.nio.file.Path is obtained using the FileSystem.getPath () method. The Path instance is an encapsulation around a file or a directory in the file system. (Note – In Unix a directory is also a file;the same logic extends in Java running on Windows and a Path instance can hold a file or a directory instance on Windows as well)

How to move file from one directory to another in Python?

We can use Files.move () API to move file from one directory to another. Following is the syntax of the move method.


4 Answers

myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system).

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

If you need a more comprehensive solution (such as wanting to move the file between disks), look at Apache Commons FileUtils#moveFile

like image 80
Thilo Avatar answered Oct 18 '22 00:10

Thilo


With Java 7 or newer you can use Files.move(from, to, CopyOption... options).

E.g.

Files.move(Paths.get("/foo.txt"), Paths.get("bar.txt"), StandardCopyOption.REPLACE_EXISTING);

See the Files documentation for more details

like image 45
micha Avatar answered Oct 18 '22 01:10

micha


Java 6

public boolean moveFile(String sourcePath, String targetPath) {

    File fileToMove = new File(sourcePath);

    return fileToMove.renameTo(new File(targetPath));
}

Java 7 (Using NIO)

public boolean moveFile(String sourcePath, String targetPath) {

    boolean fileMoved = true;

    try {

        Files.move(Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);

    } catch (Exception e) {

        fileMoved = false;
        e.printStackTrace();
    }

    return fileMoved;
}
like image 11
pvrforpranavvr Avatar answered Oct 18 '22 00:10

pvrforpranavvr


File.renameTo from Java IO can be used to move a file in Java. Also see this SO question.

like image 6
Piotr Avatar answered Oct 18 '22 02:10

Piotr