import java.io.File;
import java.io.IOException;
import java.nio.file.*;
public class TestMoveFile {
public static void main(String[] args)throws Exception {
String temp="G:\\test\\1111.txt";
String targetTemp=temp.substring(0,temp.lastIndexOf(File.separator)+1)+"backuptar";
//System.out.println(targetTemp+"111111111111111111====================");
//+temp.substring(temp.lastIndexOf("/"),temp.length());
//temp.substring(0,temp.lastIndexOf("/")+1)+"backuptar"+temp.substring(temp.lastIndexOf("/"),temp.length())
System.out.println(targetTemp);
File targetPart=new File(targetTemp);
if(!targetPart.exists()){
targetPart.mkdirs();
}
targetTemp=targetTemp+File.separator+temp.substring(temp.lastIndexOf(File.separator)+1,temp.length());
System.out.println(targetTemp+"222222222222==========================");
Path sourcePath = Paths.get(temp);
Path targetPath = Paths.get(targetTemp);
try {
Files.move(sourcePath, targetPath, StandardCopyOption.ATOMIC_MOVE);
System.out.println("成功了");
} catch (IOException e) {
System.out.println("失败了");
e.printStackTrace();
}
}
}
javaf 移动文件到指定的位置Files.move 覆盖之前同名文件
最新推荐文章于 2024-03-11 16:54:25 发布