无法删除给定目录中的文件

时间:2014-12-04 18:45:15

标签: java file-io duplicate-removal

我正在尝试编写一个删除目录中所有重复文件的程序。它目前能够检测到重复项,但我的删除代码似乎不起作用(Files.delete()返回false)。谁能告诉我为什么会这样?

当前代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.SecurityManager;

public class Duplicate {
    @SuppressWarnings("resource")
    public static boolean isDuplicate(File a, File b) throws IOException {
        FileInputStream as = new FileInputStream(a);
        FileInputStream bs = new FileInputStream(b);
        while(true) {
            int aBytes = as.read();
            int bBytes = bs.read();
            if(aBytes != bBytes) {
                return false;
            } else if(aBytes == -1) {
                System.out.println("Duplicate found: "+a.getName()+", "+b.getName());
                return true;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        File dir = new File(System.getProperty("user.dir"));
        File[] files = dir.listFiles();
        for(int i = 0; i < files.length; i++) {
            for(int j = i+1; j < files.length; j++) {
                if(isDuplicate(files[i], files[j])) {
                    String filePath = System.getProperty("user.dir").replace("\\", "/")+"/"+files[i].getName();
                    System.out.println("Deleting "+filePath);
                    File f = new File(filePath);
                    if(f.delete())
                        System.out.println(filePath+" deleted successfully");
                    else
                        System.out.println("Could not delete "+filePath);
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您关闭了文件流吗?如果文件当前处于打开状态,它将返回false是有意义的。

答案 1 :(得分:3)

除了资源问题(这当然解释了为什么你不能删除),问题是你不知道删除失败的原因 - 事实上,你有File根本不知道。

这是用java.nio.file编写的等效程序,带有资源管理:

public final class Duplicates
{

    private Duplicates()
    {
        throw new Error("nice try!");
    }

    private static boolean duplicate(final Path path1, final Path path2)
        throws IOException
    {
        if (Files.isSameFile(path1, path2))
            return true;

        final BasicFileAttributeView view1
            = Files.getFileAttributeView(path1, BasicFileAttributeView.class);
        final BasicFileAttributeView view2
            = Files.getFileAttributeView(path2, BasicFileAttributeView.class);

        final long size1 = view1.readAttributes().size();
        final long size2 = view2.readAttributes().size();

        if (size1 != size2)
            return false;
        try (
            final FileChannel channel1 = FileChannel.open(path1,
                StandardOpenOption.READ);
            final FileChannel channel2 = FileChannel.open(path2,
                StandardOpenOption.READ);
        ) {
            final ByteBuffer buf1
                = channel1.map(FileChannel.MapMode.READ_ONLY, 0L, size1);
            final ByteBuffer buf2
                = channel2.map(FileChannel.MapMode.READ_ONLY, 0L, size1);

            // Yes, this works; see javadoc for ByteBuffer.equals()
            return buf1.equals(buf2);
        }
    }

    public static void main(final String... args)
        throws IOException
    {
        final Path dir = Paths.get(System.getProperty("user.dir"));
        final List<Path> list = new ArrayList<>();

        for (final Path entry: Files.newDirectoryStream(dir))
            if (Files.isRegularFile(entry))
                list.add(entry);

        final int size = list.size();

        for (int i = 0; i < size; i++)
            for (int j = i + 1; j < size; j++)
                try {
                    if (duplicate(list.get(i), list.get(j)))
                        Files.deleteIfExists(list.get(j));
                } catch (IOException e) {
                    System.out.printf("Aiie... Failed to delete %s\nCause:\n%s\n",
                        list.get(j), e);
                }
    }
}

注意:更好的策略可能是创建一个目录,在该目录中移动您检测到的所有重复项;完成后,只需删除此目录中的所有文件,然后删除目录本身。请参阅Files.move()