windows 如何设置文件的隐藏属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5789233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set Hidden proprerty of a file
提问by Wallkan
I'm trying to create a static method that let me hide a file. I've found some possible way to do that and I wrote this:
我正在尝试创建一个让我隐藏文件的静态方法。我找到了一些可能的方法来做到这一点,我写了这个:
public static void hide(File src) throws InterruptedException, IOException {
if(System.getProperty("os.name").contains("Windows"))
{
Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
p.waitFor();
}
else
{
src.renameTo(new File(src.getParent()+File.separator+"."+src.getName()));
}
}
Unfortunatley this isn't working in windows and neither on Ubuntu... In Oracle's tuorials I've found this way
不幸的是,这在 Windows 和 Ubuntu 上都不起作用......在 Oracle 的教程中,我发现了这种方式
Path file = ...;
Files.setAttribute(file, "dos:hidden", true);
but I don't know how to use it because my JDK doesn't have the class "Path". Can anyone help me with a method that can work in unix OS and Windows?
但我不知道如何使用它,因为我的 JDK 没有“Path”类。任何人都可以帮助我使用可以在 unix OS 和 Windows 中工作的方法吗?
回答by Joachim Sauer
The Path
class was introduced in Java 7.
该Path
班是在Java 7中引入的。
Before Java 7 there was no built-in way to access properties like this, so you'll have to do something similar to what you're trying (and on Unix-y OS there is no "hidden property", but all files that start with a .
are hidden by default).
在 Java 7 之前没有内置的方式来访问这样的属性,所以你必须做一些类似于你正在尝试的事情(在 Unix-y OS 上没有“隐藏属性”,但所有文件.
默认情况下隐藏以 a 开头)。
Regarding your exec()
call there's a great (if a bit old) articlethat lists all the stuff that can go wrong and how to fix it (it's quite an involved process, unfortunately).
关于您的exec()
电话,有一篇很棒的(如果有点旧)文章列出了所有可能出错的东西以及如何修复它(不幸的是,这是一个相当复杂的过程)。
And a minor note: new File(src.getParent()+File.separator+"."+src.getName())
can be replaced by new File(src.getParent(), "." + src.getName())
, which would be a bit cleaner.
还有一个小提示:new File(src.getParent()+File.separator+"."+src.getName())
可以替换为new File(src.getParent(), "." + src.getName())
,这样会更简洁一些。
回答by Peter Lawrey
If a file as not parent, getParent() will return null. Perhaps what you wanted for unix was
如果文件不是父文件,getParent() 将返回 null。也许你想要的 Unix 是
src.renameTo(new File(src.getParent(), '.'+src.getName()));
Path
is available in Java 7.
Path
在 Java 7 中可用。
回答by Liv
you won't be able to achieve this with standard JDK code. The File class offers an isHidden method, however, it states clearly that the concept of hidden is file system dependent:
您将无法使用标准 JDK 代码实现此目的。File 类提供了一个 isHidden 方法,但是,它清楚地说明了隐藏的概念是依赖于文件系统的:
Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name begins with a period character ('.'). On Microsoft Windows systems, a file is considered to be hidden if it has been marked as such in the filesystem.
测试由此抽象路径名命名的文件是否为隐藏文件。隐藏的确切定义取决于系统。在 UNIX 系统上,如果文件名称以句点字符 ('.') 开头,则该文件被认为是隐藏的。在 Microsoft Windows 系统上,如果文件在文件系统中被标记为隐藏文件,则该文件被认为是隐藏的。
As such you need to write platform specific code (JNI?) to hide a file.
因此,您需要编写特定于平台的代码(JNI?)来隐藏文件。
回答by BullyWiiPlaza
Operating system detection code:
操作系统检测代码:
public class OperatingSystemUtilities
{
private static String operatingSystem = null;
private static String getOperatingSystemName()
{
if (operatingSystem == null)
{
operatingSystem = System.getProperty("os.name");
}
return operatingSystem;
}
public static boolean isWindows()
{
String operatingSystemName = getOperatingSystemName();
return operatingSystemName.startsWith("Windows");
}
public static boolean isMacOSX()
{
String operatingSystemName = getOperatingSystemName();
return operatingSystemName.startsWith("Mac OS X");
}
public static boolean isUnix()
{
return !isWindows();
}
}
Hiding the file code:
隐藏文件代码:
public static String hideFile(String filePath) throws IOException
{
Path path = Paths.get(filePath);
if (OperatingSystemUtilities.isWindows())
{
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
return path.toString();
} else if (OperatingSystemUtilities.isUnix())
{
String filename = path.getFileName().toString();
if (filename.startsWith("."))
{
return path.toString();
}
// Keep trying to rename
while (true)
{
Path parent = path.toAbsolutePath().getParent();
Path newPath = Paths.get(parent + File.separator + "." + filename);
// Modify the file name when it exists
if (Files.exists(newPath))
{
int lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex == -1)
{
lastDotIndex = filename.length();
}
Random random = new Random();
int randomNumber = random.nextInt();
randomNumber = Math.abs(randomNumber);
filename = filename.substring(0, lastDotIndex) + randomNumber + filename.substring(lastDotIndex, filename.length());
continue;
}
Files.move(path, newPath);
return newPath.toString();
}
}
throw new IllegalStateException("Unsupported OS!");
}
Note that you have to pay attention to avoid a file name clash when renaming to hide the file on Unix
. This is what the code implements despite it being unlikely.
请注意,在重命名以隐藏文件时,您必须注意避免文件名冲突Unix
。尽管不太可能,这就是代码实现的内容。