如何使用java隐藏文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1999437/
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 make a folder hidden using java
提问by Sunil Kumar Sahoo
I want to create a hidden folder using java application. That program should work across platform. So How to write a program which can create hidden folder.
我想使用 java 应用程序创建一个隐藏文件夹。该程序应该跨平台工作。那么如何编写一个可以创建隐藏文件夹的程序。
I have tried using
我试过使用
File newFile = new File("myfile");
newFile.mkdir();
It creates a directory which is not hidden.
它创建一个不隐藏的目录。
采纳答案by Michael Borgwardt
The concept of hidden files/folders is very OS-specific and not accessible via the Java API.
隐藏文件/文件夹的概念是特定于操作系统的,无法通过 Java API 访问。
In Linux, files and folders whose name begins with a dot are hidden per default in many programs - doing that is easy.
在 Linux 中,名称以点开头的文件和文件夹在许多程序中默认是隐藏的——这样做很容易。
In Windows, "hidden" is a special flag stored in the file system. There is no Java API for changing it; you can use Runtime.exec()
to run the attribcommand.
在 Windows 中,“隐藏”是存储在文件系统中的特殊标志。没有用于更改它的 Java API;您可以使用Runtime.exec()
来运行attrib命令。
回答by Carl Smotricz
To make a file or directory hidden under Unix, its name needs to start with a period (.
).
要在 Unix 下隐藏文件或目录,其名称需要以句点 ( .
)开头。
To make a file hidden under Windows, you need to set the 'hidden' bit in its attributes. The Java standard library doesn't offer this capability (though there is a file.isHidden()
method), and I don't offhand know any tool that does.
要在 Windows 下隐藏文件,您需要在其属性中设置“隐藏”位。Java 标准库不提供此功能(尽管有一种file.isHidden()
方法),而且我不知道有任何工具可以提供此功能。
回答by user242294
under *nix you just rename the file so that
在 *nix 下,您只需重命名文件,以便
filename = ".".filename;
回答by Ritwik Bose
You could use some form of a factory pattern for your crossplatforming needs. But what everyone else said. I'm afraid you can't quite make it plop out with one line of code, as I'm can just feel you want it to. My condolences.
您可以使用某种形式的工厂模式来满足您的跨平台需求。但是其他人都说了。恐怕你不能用一行代码把它搞得一团糟,因为我能感觉到你想要它。节哀顺变。
回答by Michel Gokan
that's OS job (and you are OS boss of course ). But you can execute attrib (Windows) command and tell OS(Windows) that you wanna make a folder "hidden".
那是操作系统的工作(当然你是操作系统的老板)。但是您可以执行 attrib (Windows) 命令并告诉 OS(Windows) 您想要“隐藏”一个文件夹。
public class Main {
public static void main(String[] args) {
try
{
Runtime rt = Runtime.getRuntime();
//put your directory path instead of your_directory_path
Process proc = rt.exec("attrib -s -h -r your_directory_path");
int exitVal = proc.exitValue();
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
回答by P?l Brattberg
If you're using Java 7 you can use the new java.nio.file.attribute
package like so:
如果您使用的是 Java 7,则可以java.nio.file.attribute
像这样使用新包:
Path path = FileSystems.getDefault().getPath("/j", "sa");
Files.setAttribute(path, "dos:hidden", true);
See more info at http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html
在http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html查看更多信息
Or, if you're using an older version of Java and/or want to do it using Runtime
, try this:
或者,如果您使用的是旧版本的 Java 和/或想要使用Runtime
,请尝试以下操作:
Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path");
回答by heemal hacker
Try following steps :
尝试以下步骤:
1. make a folder with extension **.jad** and move your videos,photos, etc
on that folder..
2. now create same folder with extenson **.jar** (ex- if u create
videos.jad then create videos.jar)
3. finished .. Videos.jad will hide .. Delete the .jar .jad will come
again