如何检查文件/目录是否为受保护的操作系统文件? [英] How to check if a file/directory is a protected OS file?

查看:116
本文介绍了如何检查文件/目录是否为受保护的操作系统文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,该项目部分地显示JTable的目录中的所有文件,包括子目录.用户可以双击子目录以使用该新目录的内容更新表.但是,我遇到了一个问题.

I'm working on a project which, in part, displays all the files in a directory in a JTable, including sub-directories. Users can double-click the sub-directories to update the table with that new directory's content. However, I've run into a problem.

我的文件列表是使用file.listFiles()生成的,它可以提取所有内容:隐藏文件,锁定文件,OS文件,整个工具包和cabo​​odle,但我无法访问所有文件.例如,我无权在"C:\ Users \ user \ Cookies \"或"C:\ ProgramData \ ApplicationData \"中进行读写.没关系,这不是获得这些访问权限的问题.相反,我不希望程序显示无法打开的目录.但是,我无权访问的目录与我所访问的目录几乎完全相同,这使得筛选它们变得非常困难.

My lists of files are generated with file.listFiles(), which pulls up everything: hidden files, locked files, OS files, the whole kit and caboodle, and I don't have access to all of them. For example, I don't have permission to read/write in "C:\Users\user\Cookies\" or "C:\ProgramData\ApplicationData\". That's ok though, this isn't a question about getting access to these. Instead, I don't want the program to display a directory it can't open. However, the directories I don't have access to and the directories I do are behaving almost exactly the same, which is making it very difficult to filter them out.

我发现的唯一行为差异是,如果我在锁定目录上调用listFiles(),则它将返回null. 这是我用作过滤器的代码块:

The only difference in behavior I've found is if I call listFiles() on a locked directory, it returns null. Here's the block of code I'm using as a filter:

for(File file : folder.listFiles())
    if(!(file.isDirectory() && file.listFiles() == null))
        strings.add(file.getName());

其中文件夹"是我正在查找的目录,字符串"是该目录中文件名称的列表.这个想法是,仅当文件是允许我编辑的文件或目录时,文件才会加载到列表中.过滤方面有效,但是有些目录包含数百个子目录,每个目录包含数百个文件,并且由于listFiles()为O(n),所以这不是可行的解决方案(list()是还是更好).

Where 'folder' is the directory I'm looking inside and 'strings' is a list of names of the files in that directory. The idea is a file only gets loaded into the list if it's a file or directory I'm allowed to edit. The filtering aspect works, but there are some directories which contain hundreds of sub-directories, each of which contains hundreds more files, and since listFiles() is O(n), this isn't a feasible solution (list() isn't any better either).

但是, file.isHidden()返回false

However, file.isHidden() returns false

canWrite()/canRead()/canExecute()返回true

canWrite()/canRead()/canExecute() return true

getPath()返回与getAbsolutePath()和getCanonicalPath()相同

getPath() returns the same as getAbsolutePath() and getCanonicalPath()

createNewFile()对于所有内容都返回false,即使我知道的目录也可以.另外,即使确实可行,我还是要避免使用该解决方案.

createNewFile() returns false for everything, even directories I know are ok. Plus, that's a solution I'd really like to avoid even if that worked.

是否有一些我不知道的方法或实现可以帮助我查看此目录是否可访问,而无需解析其所有内容?

Is there some method or implementation I just don't know to help me see if this directory is accessible without needing to parse through all of its contents?

(我正在运行Windows 7 Professional,并且正在使用Eclipse Mars 4.5.2,并且File的所有实例都是java.io.File).

(I'm running Windows 7 Professional and I'm using Eclipse Mars 4.5.2, and all instances of File are java.io.File).

推荐答案

您遇到的问题是您正在处理File.从所有人的角度来看,从2016年开始,实际上是从2011年(Java 7推出以来)开始,它已被JSR 203取代.

The problem you have is that you are dealing with File. By all accounts, in 2016, and, in fact, since 2011 (when Java 7 came out), it has been superseded by JSR 203.

现在,什么是JSR 203?它是一种全新的API,可以处理任何文件系统和文件系统对象.并且扩展了文件系统"的定义,以包括您在本地计算机上找到的文件(JDK所谓的默认文件系统")以及您可能使用的其他文件系统.

Now, what is JSR 203? It is a totally new API to deal with anything file systems and file system objects; and it extend the definition of a "file system" to include what you find on your local machine (the so called "default filesystem" by the JDK) and other file systems which you may use.

有关如何使用它的示例页面:此处

Sample page on how to use it: here

此API的许多优点是,它授予对以前无法访问的元数据的访问权限;例如,您在注释中特别提到了一种情况,您想知道Windows将哪些文件视为系统文件".

Among the many advantages of this API is that it grants access to metadata which you could not access before; for instance, you specifically mention the case, in a comment, that you want to know which files Windows considers as "system files".

这是您可以执行的操作:

This is how you can do it:

// get the path
final Path path = Paths.get(...);
// get the attributes
final DosAttributes attrs = Files.readAttributes(path, DosFileAttributes.class);
// Is this file a "system file"?
final boolean isSystem = attrs.isSystem();


现在,什么是Paths.get()?如前所述,该API可让您一次访问多个文件系统.名为 FileSystems 的类给出可以访问JDK可见的所有文件系统(包括创建新的文件系统),并且始终存在的默认文件系统由FileSystems.getDefault()赋予.


Now, what is Paths.get()? As mentioned previously, the API gives you access to more than one filesystem at a time; a class called FileSystems gives access to all file systems visible by the JDK (including creating new filesystems), and the default file system, which always exists, is given by FileSystems.getDefault().

FileSystem实例还使您可以使用

A FileSystem instance also gives you access to a Path using FileSystem#getPath.

将其组合起来,您会发现这两个是等效的:

Combine this and you get that those two are equivalent:

Paths.get(a, b, ...)
FileSystems.getDefault().getPath(a, b, ...)


关于异常:File对异常的处理非常差.仅举两个例子:


About exceptions: File handles them very poorly. Just two examples:

    如果无法创建文件,
  • File#createNewFile将返回false;
  • 如果File对象指向的目录的内容由于某种原因而无法读取,则
  • File#listFiles将返回null.
  • File#createNewFile will return false if the file cannot be created;
  • File#listFiles will return null if the contents of the directory pointed by the File object cannot be read for whatever reason.

JSR 203没有这些缺点,甚至还有更多缺点.让我们采用两种等效的方法:

JSR 203 has none of these drawbacks, and does even more. Let us take the two equivalent methods:

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