File.listFiles() 并区分物理文件夹和虚拟文件夹

发布于 2024-09-07 15:24:07 字数 429 浏览 1 评论 0原文

File.listFiles() 方法列出文件夹的内容。然而,在 Windows 上,当您在根驱动器上调用该方法时,它还会生成一些(缺乏更好的短语)“虚拟”文件夹(例如“文档和设置”,在最近的 Windows 版本中已被“用户”取代) 。

如果您随后为这些“虚拟”文件夹之一调用 listFiles(),它始终返回 null。这就是我的问题所在,因为我想递归地遍历所有文件夹。我需要一种方法来过滤掉这些文件夹,最好不要通过检查它们的名称...

还有一些普通用户看不到的其他文件夹(例如“系统卷信息”或该驱动器的回收站文件夹) ),我很高兴找到一种过滤掉这些名称而不将名称列入黑名单的方法。

不幸的是,这些文件夹的行为就像真实的文件夹一样。我正在寻找类似 File.isSystemFolder() 的方法。

我真的很感谢任何建议

The File.listFiles() method lists the contents of a folder. On windows however, when you call the method on your root drive, it also yields some - in lack of a better phrase - "virtual" folders (Like "Documents and Settings", which got replaced by "Users" in recent windows versions).

If you then call listFiles() for one of those "virtual" folders, it always returns null. And that's where my problem is, as I want to recursively walk through all the folders. I need a way to filter those folders out and preferably not by checking their names...

There are also some additional folders, that the normal user doesn't see (like "System Volume Information" or the Recycle-bin folder for that drive), and I would be glad to find a method of filtering those out as well without blacklisting the names.

Unfortunately, those folders behave just like real ones. What I'm looking for is something like a File.isSystemFolder()-method.

I'm really thankful for any advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一花一树开 2024-09-14 15:24:07

您可以通过测试 File#isHidden() 返回true

我不确定“虚拟”文件夹。我认为只有使用 New IO 2 即将在 Java 7 中推出。我必须首先在 Vista/Win7 机器上检查这一点(我目前使用的是 XP)。


更新:我在我们的 2K3 服务器上进行了快速测试,文档和设置默认情况下也会为 File#isHidden( )用户则不然。你也可以利用它。

You can filter those hidden system folders by testing if File#isHidden() returns true.

I am not sure about "virtual" folders. I think distinguishing them is only possible with the New IO 2 which is coming in Java 7. I'll have to check that first yet on a Vista/Win7 machine (I'm currently on XP).


Update: I did a quick test at our 2K3 Server, the Documents and Settings by default also returns true for File#isHidden() while Users don't. You could make use of that as well.

或十年 2024-09-14 15:24:07

我认为您不会幸运地使用 File.isSystemFolder 方法,因为我无法想象对 JVM 强加一个足够广泛的“系统文件夹”定义会很容易一系列能够正确有效地实施的操作系统。正如 Balus 所建议的,检查非隐藏文件夹是一个良好的开始,并且本身就足够了(因为这复制了他们在资源管理器 shell 中看到的内容)。

关于“虚拟”文件夹 - 首先,您确定它们是虚拟的吗? C:\Documents and Settings(或更高版本的C:\Users)不是此文件夹的真实、规范路径吗?如果不是,那么您可以通过查看是否 getCanonicalPath().equals(getAbsolutePath()) 轻松地在 Windows 上清除这些内容(只要您准备好应对一些多余的误报) >. 和 .. )。

如果这不起作用,那么它们只是普通文件夹 - 您需要考虑这些文件夹具有哪些逻辑属性使它们成为“虚拟”。或者换句话说,提出一些可以用 File 上的方法来表达的谓词,捕获您想要过滤的内容。根据您的英文描述,即使我现在也不确定什么适合,什么不适合。

I don't think you'll be in luck with a File.isSystemFolder method, because I can't imagine it would be easy to impose a definition of "system folder" that's broad enough for JVMs on a range of operating systems to be able to implement correctly and usefully. As Balus suggests, checking for non-hidden folders is a good start and might well be enough on its own (since this replicates what they would see in an explorer shell).

About the "virtual" folders - first, are you sure that they're virtual? Is C:\Documents and Settings (or later, C:\Users) not the real, canonical path for this folder? If it's not, then you could easily weed these out on windows by seeing whether getCanonicalPath().equals(getAbsolutePath()) (so long as you're prepared for a few false positives with superfluous .s and ..s).

If this doesn't work, then they're just normal folder - you need to think about what logical properties these folders have that make them "virtual". Or in other words, come up with some predicate that can be expressed in terms of the methods on File that captures what you want to filter. Even I'm not sure right now quite what would and would not fit, based on your English description.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文