Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a File/Folder Hidden on Windows with Java

I need to make files and folders hidden on both Windows and Linux. I know that appending a '.' to the front of a file or folder will make it hidden on Linux. How do I make a file or folder hidden on Windows?

like image 225
Kryten Avatar asked Aug 18 '09 16:08

Kryten


People also ask

How do I create a hidden folder in Java?

To make a file or directory hidden under Unix, its name needs to start with a period ( . ). 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.

How do I make a hidden folder in Windows?

To hide one or more files or folders, select the files or folders, right-click on them, and select Properties. On the General tab on the Properties dialog box, check the Hidden box in the Attributes section.

How do you get a directory is hidden or not in Java?

io. File. isHidden() is used to check whether the file or directory specified by the abstract path name is hidden in Java. This method returns true if the file or directory specified by the abstract path name is hidden and false otherwise.


2 Answers

The functionality that you desire is a feature of NIO.2 in the upcoming Java 7.

Here's an article describing how will it be used for what you need: Managing Metadata (File and File Store Attributes). There's an example with DOS File Attributes:

Path file = ...; try {     DosFileAttributes attr = Attributes.readDosFileAttributes(file);     System.out.println("isReadOnly is " + attr.isReadOnly());     System.out.println("isHidden is " + attr.isHidden());     System.out.println("isArchive is " + attr.isArchive());     System.out.println("isSystem is " + attr.isSystem()); } catch (IOException x) {     System.err.println("DOS file attributes not supported:" + x); } 

Setting attributes can be done using DosFileAttributeView

Considering these facts, I doubt that there's a standard and elegant way to accomplish that in Java 6 or Java 5.

like image 115
Marian Avatar answered Oct 02 '22 23:10

Marian


For Java 6 and below,

You will need to use a native call, here is one way for windows

Runtime.getRuntime().exec("attrib +H myHiddenFile.java"); 

You should learn a bit about win32-api or Java Native.

like image 35
Andrew Avatar answered Oct 03 '22 00:10

Andrew