try {
            File realFile = new File(file);

            boolean rename = realFile.renameTo(new File("C:\\Dummy Files 2\\" + file));

            if (rename) {
                System.out.println(file + " was moved succesfully");
                boolean delete = realFile.delete();

                if (delete) {
                    System.out.println(realFile.getAbsolutePath() + " was deleted");
                } else {
                    System.out.println(realFile.getAbsolutePath() + " could not be deleted");
                }
            } else {
                System.out.println(file + " could not be moved");
            }

Above is the snippet of code I am using to move files. The files don't get moved, and they don't get deleted, as a result.

System.out.println(file + " could not be moved");

This is printed out every time. I have no idea what I'm doing wrong. Help or advice would be greatly appreciated.

Recommended Answers

All 2 Replies

This works for me:

File fromFile = new File("FromFolder\\MoveMe.txt");
      File toFile = new File("ToFolder\\MoveMe.txt");
      System.out.println("renameTo = " + fromFile.renameTo(toFile));  // renameTo = true

What else could be wrong?

Apologies, I seem to have created the same thread twice o.O

Nonetheless, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.