【积累】java中,文件的移动

本文介绍了一个使用Java实现的文件移动方法。该方法首先检查目标路径是否存在,若不存在则创建目录,然后通过renameTo方法完成文件的移动操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在java中,我们对文件的操作,包含了很多方式,下面就是一个移动文件的方法,积累下来,做个记录
 1 public static boolean moveFileForBack(String srcFile, String destPath) {
 2         try {
 3             if (!(new File(destPath).isDirectory())) {
 4                 new File(destPath).mkdir();
 5                 // new File("D:/java/newdata/2003-04-14/").mkdir();
 6             }
 7         } catch (SecurityException e) {
 8             System.out.println("can not make directory");
 9         }
10         File file = new File(srcFile);
11         File dir = new File(destPath);
12         boolean success = file.renameTo(new File(dir, file.getName()));
13         return success;
14     } 
        还有一个,就是移动的位置,如果没有文件夹,我们就创建一个,是上面代码的深入探究。
 1 public static boolean moveFileForBack(String srcFile, String destPath) {
 2         try {
 3             if (!(new File(destPath).isDirectory())) {
 4                 new File(destPath).mkdir();
 5                 // new File("D:/java/newdata/2003-04-14/").mkdir();
 6             }
 7         } catch (SecurityException e) {
 8             System.out.println("can not make directory");
 9         }
10         File file = new File(srcFile);
11         File dir = new File(destPath);
12         boolean success = file.renameTo(new File(dir, file.getName()));
13         return success;
14     }
15 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值