Move file in Java
To migrate any file in Java, in Java 6 or older Java versions, we can use the renameTo() method located in the File object to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.javaexample; import java.io.File; public class MoveFile { public static void main(String[] args) { File source = new File("F://source.txt"); File dest = new File("E://dest.txt"); if (source.renameTo(dest)) { System.out.println("Move file " + source.getAbsolutePath() + " to " + dest.getAbsolutePath() + " success."); } } } |
Result:
1 |
Move file F:\source.txt to E:\dest.txt success |
From Java 7 onwards, we have another way to move a file, which is to… Read More