java删除文件失败

        前几天做用java导入execl数据,程序是去年一个新员工写的,当时测试数据是可以导入的,再多也没研究里面的方法合理不、能否优化都没有再考虑这些。
        导入的文件比较多,我就想给你加个把成功导入的execl文件转移到另外一个文件夹下,转移是没有问题,可是删除就出了问题,看删除方法是没有问题,就开始看他写的代码,发现读取execl文件的流没有关闭,导致文件无法。
        我就在try catch finally的finally中把这个流关闭,然后在进行我文件复制和删除。
     
        我的复制文件方法和删除文件方法如下
 1      public   static   boolean  copyFile(String oldPath, String newPath)  {
 2        try {
 3            int bytesum = 0;
 4            int byteread = 0;
 5            File oldfile = new File(oldPath);
 6            if (oldfile.exists()) // 文件存在时
 7                InputStream inStream = new FileInputStream(oldPath); // 读入原文件
 8                FileOutputStream fs = new FileOutputStream(newPath);
 9                byte[] buffer = new byte[1444];
10                int length;
11                while ((byteread = inStream.read(buffer)) != -1{
12                    bytesum += byteread; // 字节数 文件大小
13                    // System.out.println(bytesum);
14                    fs.write(buffer, 0, byteread);
15                }

16                inStream.close();
17                fs.close();
18            }

19            return true;
20        }
 catch (Exception e) {
21            // System.out.println("复制单个文件操作出错");
22            e.printStackTrace();
23            return false;
24        }

25    }

26
27      /** */ /**
28     * 删除文件或者文件夹,对于文件夹遍历其子文件夹进行递归删除
29     * 
30     * @param f -
31     *            File对象
32     * @return 删除是否成功
33     */

34      public   static   boolean  deleteFile(File f)  {
35        if (f.exists()) {
36            if (f.isFile())
37                return f.delete();
38            else if (f.isDirectory()) {
39                File[] files = f.listFiles();
40                for (int i = 0; i < files.length; i++{
41                    if (!deleteFile(files[i]))
42                        return false;
43                }

44                return f.delete();
45            }
 else
46                return false;
47        }
 else
48            return false;
49    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值