如何使用 DirectoryStream.Filter 过滤隐藏文件 [英] How do you filter hidden files using DirectoryStream.Filter

查看:73
本文介绍了如何使用 DirectoryStream.Filter 过滤隐藏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NIO 类过滤隐藏文件.

I am trying to filter hidden files using the NIO classes.

当我在 Windows 10 上运行附加的代码时,我得到以下输出:

When I run the attached code on Windows 10 I get the following output:

Files:
        c:\Documents and Settings
        c:\PerfLogs
        c:\Program Files
        c:\Program Files (x86)
        c:\Users
        c:\Windows

Paths:
        c:\$Recycle.Bin
        c:\Config.Msi
        c:\Documents and Settings
        c:\Intel
        c:\IntelOptaneData
        c:\OEM
        c:\OneDriveTemp
        c:\PerfLogs
        c:\Program Files
        c:\Program Files (x86)
        c:\ProgramData
        c:\Recovery
        c:\System Volume Information
        c:\Users
        c:\Windows

文件下显示的列表(使用旧的 File.listFiles(FileFilter) 方法)是我在 Windows 文件资源管理器中看到的列表,也是我期望看到的(除了 Document 和设置,我知道如何解决)

The list displayed under Files (using the old File.listFiles(FileFilter) method) is the list I see in Windows File Explorer and is what I am expecting to see (except for the Document and Setting and I know how to fix that)

  1. 为什么 NIO 的方法没有以同样的方式过滤隐藏文件?
  2. 如何让 NIO 过滤保持一致?

测试代码如下:

import java.io.*;
import java.nio.file.*;

public class ListFilesNIO
{
    public static void main(String[] args) throws Exception
    {
        String directory = "c:\\";

        //  Use old File I/O

        FileFilter fileFilter = new FileFilter()
        {
            @Override
            public boolean accept(File entry)
            {
                if (entry.isHidden()) return false;

                return true;
            }
        };

        System.out.println("Files:");
        File[] files = new File( directory ).listFiles( fileFilter );

        for (File file : files)
        {
            System.out.println( "\t" + file );
        }

        //  Use NIO

        DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
        {
            @Override
            public boolean accept(Path entry) throws IOException
            {
                if (Files.isHidden( entry )) return false;

                return true;
            }
        };

        System.out.println();
        System.out.println("Paths:");
        DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get( directory ), pathFilter);

        for (Path path : paths)
        {
            System.out.println( "\t" + path );
        }
    }
}

注意:当我在没有过滤器的情况下运行代码时,在这两种情况下都会显示 18 个文件.所以第一种方法是过滤12个隐藏文件,第二种方法只过滤3个文件.

Note: when I run the code without the filter, in both cases 18 files are displayed. So the first approach is filtering 12 hidden files and the second approach is only filtering 3 files.

推荐答案

这不是 bug,而是自 jdk7 以来已知的功能(!),Windows 隐藏目录未被检测为隐藏,请参阅此 bug 和这个 one(修复 jdk13).

It's not a bug but a feature(!) known since jdk7, Windows hidden directory are not detected as hidden, see this bug and this one (fix jdk13).

作为一种解决方法,您可以这样做:

As a workaround, you can do this :

import java.nio.file.attribute.DosFileAttributes;
...
DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
        {
            @Override
            public boolean accept(Path entry) throws IOException
            {
               DosFileAttributes attr = Files.readAttributes(entry, DosFileAttributes.class);
               return !attr.isHidden();
            }
        };

这篇关于如何使用 DirectoryStream.Filter 过滤隐藏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆