Disclosure: This text could comprise affiliate hyperlinks. If you buy, we could earn a small fee.
On Home windows, that you must straight execute the DOS command as acknowledged in how one can execute shell instructions from Java by utilizing Runtime.getRuntime.exec(“command”) to make the file hidden in Home windows. Right here is an instance of creating information hidden in Home windows utilizing Java Program.
Runtime.getRuntime().exec(“attrib +H HiddenFileExample.java”);
And, If you’re new to the Java world then I additionally suggest you undergo these on-line Java programming programs to study Java in a greater and extra structured means. This is among the finest and up-to-date programs to study Java on-line.
Utilizing Runtime.exec()
The way to Conceal a File in Java with Instance

void setHiddenProperty(File file) throws InterruptedException, IOException {
Course of p = Runtime.getRuntime().exec(“attrib +H ” + file.getPath());
p.waitFor();
}
right here name to p.waitFor() is elective however calling waitFor() ensures that file will seem hidden as quickly as perform exit.
Now we are going to see code instance to cover a file in Java7 utilizing new java.nio.file bundle and new lessons like “Path” which encapsulate file path in varied working programs and can be utilized to function on information, directories, and different forms of information in Java.
Code Instance of creating a file hidden in JDK7
Here’s a full code instance of hiding a File in home windows from Java7 new options.
Path path = FileSystems.getDefault().getPath(“listing”, “hidden.txt”);
Boolean hidden = path.getAttribute(“dos:hidden”, LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
path.setAttribute(“dos:hidden”, Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
That’s all on how one can cover information in Java by utilizing both Runtime.exec() or new java.nio.file bundle launched in JDK7. let me know if you realize some other means of creating a file hidden from the Java program and we are able to embrace that instance right here.
Another Java Tutorial you might like: