Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a folder hidden using java

Tags:

java

io

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.

I have tried using

File newFile = new File("myfile");
newFile.mkdir();

It creates a directory which is not hidden.

like image 284
Sunil Kumar Sahoo Avatar asked Jan 04 '10 13:01

Sunil Kumar Sahoo


1 Answers

If you're using Java 7 you can use the new java.nio.file.attribute package like so:

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

Or, if you're using an older version of Java and/or want to do it using Runtime, try this:

Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path"); 

See more info on cmd and attrib.

like image 170
Pål Brattberg Avatar answered Oct 21 '22 15:10

Pål Brattberg