当前位置:   article > 正文

javaf 移动文件到指定的位置Files.move 覆盖之前同名文件

files.move 覆盖
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.*;
  4. public class TestMoveFile {
  5. public static void main(String[] args)throws Exception {
  6. String temp="G:\\test\\1111.txt";
  7. String targetTemp=temp.substring(0,temp.lastIndexOf(File.separator)+1)+"backuptar";
  8. //System.out.println(targetTemp+"111111111111111111====================");
  9. //+temp.substring(temp.lastIndexOf("/"),temp.length());
  10. //temp.substring(0,temp.lastIndexOf("/")+1)+"backuptar"+temp.substring(temp.lastIndexOf("/"),temp.length())
  11. System.out.println(targetTemp);
  12. File targetPart=new File(targetTemp);
  13. if(!targetPart.exists()){
  14. targetPart.mkdirs();
  15. }
  16. targetTemp=targetTemp+File.separator+temp.substring(temp.lastIndexOf(File.separator)+1,temp.length());
  17. System.out.println(targetTemp+"222222222222==========================");
  18. Path sourcePath = Paths.get(temp);
  19. Path targetPath = Paths.get(targetTemp);
  20. try {
  21. Files.move(sourcePath, targetPath, StandardCopyOption.ATOMIC_MOVE);
  22. System.out.println("成功了");
  23. } catch (IOException e) {
  24. System.out.println("失败了");
  25. e.printStackTrace();
  26. }
  27. }
  28. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/325272
推荐阅读
相关标签
  

闽ICP备14008679号