我想使用Java应用程序创建一个隐藏文件夹。该程序应该在各个平台上都能工作。那么如何编写一个可以创建隐藏文件夹的程序。
我已经尝试使用。
我已经尝试使用。
File newFile = new File("myfile");
newFile.mkdir();
它创建一个非隐藏的目录。如果你使用的是Java 7,你可以像下面这样使用新的java.nio.file.attribute
包:
Path path = FileSystems.getDefault().getPath("/j", "sa");
Files.setAttribute(path, "dos:hidden", true);
查看更多信息,请访问http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html
或者,如果您使用的是较旧版本的Java和/或希望使用Runtime
进行操作,请尝试以下方法:
Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path");
隐藏文件/文件夹的概念是具体操作系统相关的,Java API无法访问。
在Linux中,以点开头的文件和文件夹在许多程序中默认为隐藏-这很容易实现。
在Windows中,“隐藏”是存储在文件系统中的特殊标记。没有Java API可以更改它;您可以使用Runtime.exec()
运行attrib命令。
filename = ".".filename;
在Unix下,要使文件或目录隐藏,其名称需要以句点(.
)开头。
在Windows下,要使文件隐藏,您需要在其属性中设置“隐藏”位。 Java标准库不提供此功能(尽管有一个file.isHidden()
方法),我也不知道有任何工具可以做到。
这是操作系统的工作(当然你是操作系统的老板)。但是你可以执行 attrib 命令(在Windows中),告诉操作系统(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();
}
}
}
proc.waitFor()
иҖҢдёҚжҳҜproc.exitValue()
пјҢеӣ дёәеҗҺиҖ…дјҡеҜјиҮҙIllegalThreadStateException: process has not exited
ејӮеёёгҖӮ - Ondřej Bouda您可以使用某种工厂模式来满足跨平台需求。但是就像其他人说的一样,我很遗憾地告诉您,您无法只用一行代码实现它,我能感觉到您渴望这样。我的慰问。
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
Runtime.getRuntime().exec("attrib +H " + dir.getAbsolutePath());
Runtime.getRuntime().exec("chflags hidden " + dir.getAbsolutePath());