简体   繁体   English

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

[英]File.listFiles() and distinguishing physical folders from virtual ones

The File.listFiles() method lists the contents of a folder. File.listFiles()方法列出文件夹的内容。 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). 然而,在Windows上,当您在根驱动器上调用该方法时,它还会产生一些 - 缺少更好的短语 - “虚拟”文件夹(如“文档和设置”,在最近的Windows版本中被“用户”替换) 。

If you then call listFiles() for one of those "virtual" folders, it always returns null. 如果您随后为其中一个“虚拟”文件夹调用listFiles(),它将始终返回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. 普通用户还没有看到一些额外的文件夹(如“系统卷信息”或该驱动器的Recycle-bin文件夹),我很乐意找到一种方法来过滤掉那些文件夹而不会出现黑名单名字。

Unfortunately, those folders behave just like real ones. 不幸的是,这些文件夹的行为与真实文件夹一样。 What I'm looking for is something like a File.isSystemFolder() -method. 我正在寻找的东西就像File.isSystemFolder() - File.isSystemFolder()

I'm really thankful for any advice 我真的很感谢任何建议

You can filter those hidden system folders by testing if File#isHidden() returns true . 您可以通过测试File#isHidden()返回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). 我认为只有使用Java 7中的New IO 2才能区分它们。我必须先在Vista / Win7机器上检查它(我目前在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. 更新 :我在2K3服务器上进行了快速测试,默认情况下, 文档和设置File#isHidden()也返回true ,而用户则不然。 You could make use of that as well. 你也可以利用它。

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. 我不认为你会对File.isSystemFolder方法好运,因为我无法想象对一系列操作系统上的JVM进行足够宽泛的“系统文件夹”定义会很容易能够正确有效地实施。 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). 正如Balus建议的那样,检查非隐藏文件夹是一个良好的开端,并且可能已经足够了(因为这会复制他们在资源管理器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? C:\\Documents and Settings (或更高版本, C:\\Users )不是此文件夹的真实,规范路径吗? 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). 如果不是,那么你可以很容易地看到这些杂草出来的窗口是否getCanonicalPath().equals(getAbsolutePath())只要你用多余的一些假阳性准备. S和.. 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. 或者换句话说,提出一些谓词,可以用File上的方法表示,它捕获你想要过滤的内容。 Even I'm not sure right now quite what would and would not fit, based on your English description. 根据你的英文描述,即使我现在还不确定那些适合和不适合的内容。

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

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