Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

Java – how to move files using FileUtils

Home/ Questions/Q 497
Next
Answered
Java - how to move files using FileUtils
0chrissie0
0chrissie0 Begginer

I am a Java developer with intermediate level proficiency. I am currently trying to move numerous files of different formats from one directory to another on my server, but I’m running into some issues. I have tried using the FileUtils class and specifying the source file path and destination file path, but the files are not being moved as expected.

Here’s the code I am using:

File sourceFolder = new File("/home/user/Desktop/sourceFolder");
File destFolder = new File("/home/user/Desktop/destFolder");
try {
FileUtils.moveDirectoryToDirectory(sourceFolder, destFolder, false);
} catch (IOException e) {
e.printStackTrace();
}

However, when I run this code, all of the files in the sourceFolder are deleted, but they are not moved to the destFolder. I cannot seem to figure out why this is happening. I have checked the directory paths multiple times and they are correct, so I do not believe that is the issue.

One possible cause of the problem could be related to file permissions, but I am not sure how to check for that in Java. Another possible cause of the issue could be the use of the FileUtils.moveDirectoryToDirectory method when I should be using the FileUtils.moveFileToDirectory method instead. However, I’m not sure how to specify all the files in the source directory as the file to be moved when using the moveFileToDirectory method. Any help on this issue would be greatly appreciated.

file manipulationfile-utilsjavamove files
  • 498
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    zhengal Teacher
    2019-03-15T18:36:13+00:00Added an answer about 4 years ago

    Hello there,

    When it comes to moving files in Java, you have a few options. If you want to use a third-party library, one popular choice is Apache Commons IO. This library provides a lot of useful file-related utilities, including methods for moving files. One such method is FileUtils.moveFile. Here’s an example of how to use it:


    File sourceFile = new File("path/to/source/file");
    File destFile = new File("path/to/destination/file");

    try {
    FileUtils.moveFile(sourceFile, destFile);
    } catch (IOException e) {
    // handle the exception appropriately
    }

    Another option is to use the built-in methods provided by the Java standard library. You can use the renameTo method of the File class to move a file from one location to another.


    File sourceFile = new File("path/to/source/file");
    File destFile = new File("path/to/destination/file");

    if (!sourceFile.renameTo(destFile)) {
    // handle the error appropriately
    }

    It’s worth noting that there are some limitations to using renameTo. For example, it may not work correctly if you’re trying to move a file across different file systems. In that case, you’ll need to use a more robust solution like the one provided by Apache Commons IO.

    Another thing to keep in mind is that when you move a file, you’re essentially removing it from its original location and creating a new copy in the destination location. Depending on the size of the file, this can take a significant amount of time and consume a lot of system resources. If performance is a concern, you may need to consider alternative approaches (such as copying the file in chunks).

    In summary, moving files in Java is a task with a few different approaches. Using a third-party library like Apache Commons IO provides a robust and easy-to-use solution, but the built-in methods provided by the Java standard library can also be sufficient in certain scenarios. Be mindful of potential limitations and performance concerns when choosing an approach.

    • 169
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. taylaallen97 Teacher
    2019-03-31T19:09:22+00:00Added an answer about 4 years ago

    One way to move files in Java is by using the NIO2 library. You can do this by creating a Path object for the source file and the destination file, and then calling the `Files.move()` method. Here’s an example:

    “`java
    Path source = Paths.get(“/path/to/source/file”);
    Path destination = Paths.get(“/path/to/destination/file”);
    Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
    “`

    This will move the file located at `/path/to/source/file` to `/path/to/destination/file`, replacing any existing file at the destination. The `REPLACE_EXISTING` option specifies that if the destination file already exists, it should be replaced. If you don’t want to replace the file, you can omit this option.

    Alternatively, you can use the `File.renameTo()` method to move a file. Here’s an example:

    “`java
    File source = new File(“/path/to/source/file”);
    File destination = new File(“/path/to/destination/file”);
    source.renameTo(destination);
    “`

    This will move the file located at `/path/to/source/file` to `/path/to/destination/file`. However, there are some limitations to using `File.renameTo()`. In particular, it may not work correctly on all platforms, and it may not work correctly if the source and destination files are on different drives or file systems.

    Overall, I recommend using the NIO2 library to move files in Java, as it provides a more robust and platform-independent solution.

    • 72
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. edobrovics Teacher
    2019-03-31T19:03:18+00:00Added an answer about 4 years ago

    When you are dealing with file operations, there are many ways to accomplish the same task. One alternative way to move a file between directories in Java would be to use the `Files` class.

    Here is an example:

    “`
    Path source = Paths.get(“source-file-path”);
    Path dest = Paths.get(“destination-file-path”);

    try {
    Files.move(source, dest);
    } catch (IOException e) {
    e.printStackTrace();
    }
    “`

    This code snippet moves the file located at `source-file-path` to `destination-file-path`. The `Files.move` method can handle different types of copy options, such as `REPLACE_EXISTING` and `ATOMIC_MOVE`, that can help you tailor the operation to your particular needs.

    The `Files` class also provides many other useful methods for managing files and directories, which you may find helpful in your project.

    • 39
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.