Tuesday, March 1, 2011

Move a file from one folder to another folder in Java

You can give another name for moved file as in this example.
(i.e: My original file name is "a.txt" and after moving its name is changed to "b.txt")
try {
  File fileBeforeRename = new File("c:\\a.txt");
  File fileAfterRename = new File("D:\\NewFolder\\b.txt");
  boolean result = fileBeforeRename.renameTo(fileAfterRename);
  System.out.println(result);
} catch (Exception e) {
  e.printStackTrace();
}

No comments:

Post a Comment