简体   繁体   English

File.delete()不会删除文件

[英]File.delete() won't delete the file

There's something going on here that I don't understand. 这里发生了一些我不理解的事情。 This code deletes all the files in the "stuff"directory: 此代码删除“ stuff”目录中的所有文件:

public static void main(String[] args) throws Exception {

    File dire = new File("C:\\Users\\spacitron\\Desktop\\Stuff");

    for (File doc : dire.listFiles()) {
        doc.delete();
    }

}

However it won't work if I try to do something useful with it, such as only deleting duplicate files: 但是,如果我尝试对其进行一些有用的操作(例如仅删除重复的文件),它将无法正常工作:

public static void main(String[] args) throws Exception {

    File dire = new File("C:\\Users\\spacitron\\Desktop\\Stuff");
    ArrayList<String> hashes = new ArrayList<>();
    for (File doc : dire.listFiles()) {
        String docHash = getHash(doc);
        if (hashes.contains(docHash)) {
            doc.delete();
        } else {
            hashes.add(docHash);
        }
    }

}

public static String getHash(File d) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA1");
        FileInputStream inStream = new FileInputStream(d);
        DigestInputStream dis = new DigestInputStream(inStream, md);
        BufferedInputStream bis = new BufferedInputStream(dis);
        while (true) {
            int b = bis.read();
            if (b == -1)
                break;
        }
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    }

    BigInteger bi = new BigInteger(md.digest());

    return bi.toString(16);
}

What gives? 是什么赋予了?

You need to close your input streams in a finally block would be best, These will be accessing you files still preventing them from being deleted as they are in use 您最好在finally块中关闭输入流,这将是最好的选择,这些将访问您的文件,但仍防止它们在使用中被删除

    FileInputStream inStream = null;
    DigestInputStream dis = null;
    BufferedInputStream bis = null;

    try {
       md = MessageDigest.getInstance("SHA1");
       inStream = new FileInputStream(d);
       dis = new DigestInputStream(inStream, md);
       bis = new BufferedInputStream(dis);
       while (true) {
            int b = bis.read();
            if (b == -1)
                break;
            }
   } catch (NoSuchAlgorithmException | IOException e) {
       e.printStackTrace();
   } finally {
       try{
          if(inStream!= null)
              inStream.close();
          if(dis != null)
              dis.close();
          if(bis != null)
              bis.close()
      } catch (Exception ex){
          ex.printStackTrace()
      }         
    }

Windows does not permit deleting files that are open, unless they are opened with a special flag that is unavailable when programming in Java. Windows不允许删除已打开的文件,除非使用特殊标志打开这些文件,而该特殊标志在Java编程时不可用。 While this code would work on a Unix system, on Windows it won't. 尽管此代码可在Unix系统上运行,但在Windows上则无法。

Closing open files is a good idea in general because operating systems impose a limit on the number of files that an application can have open at any given time. 通常,关闭打开的文件是一个好主意,因为操作系统会限制应用程序在任何给定时间可以打开的文件数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM