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.

Edited by P00dle: n/a

8 Years
Discussion Span
Last Post by P00dle

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.