Make a File/Folder Hidden on Windows with Java

Make a File/Folder Hidden on Windows with Java

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.

How to make a folder hidden using java

The concept of hidden files/folders is very OS-specific and not accessible via the Java API.

In Linux, files and folders whose name begins with a dot are hidden per default in many programs - doing that is easy.

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 attrib command.

Hide a file using java

Pure Java 7 way for DOS files:

Path file = Paths.get("fileToHide.dat");
Files.setAttribute(file, "dos:hidden", true);

http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

How to create a hidden file in any OS using Java

What I did was to creat a OSValidator and for each OS I encode my file and save it in a Application Dir (windows: appdata, mac: Application Suport). It seemed to be the easiest to do.

public class OSValidator {
private static String OS = System.getProperty("os.name").toLowerCase();

public static boolean isWindows(){
return (OS.indexOf("win")>=0);
}

public static boolean isMac(){
return (OS.indexOf("mac")>=0);
}

public static boolean isUnix() {
return (OS.indexOf("nix") >=0 || OS.indexOf("nux") >=0 || OS.indexOf("aix") >= 0);
}

public static boolean isSolaris(){
return (OS.indexOf("sunos") >=0);
}
}

if (OSValidator.isWindows()) {
System.out.println("This is Windows");
file = new File(System.getenv("APPDATA") + File.separator + "TargetApp" + File.separator +"config.json");

if (!file.exists()) {
try {
FileUtils.forceMkdir(file.getParentFile());
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
}

} else if (OSValidator.isMac()) {
System.out.println("This is Mac");

file = new File(System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support"
+ File.separator + "config.json");
}

How to hide a file that even if Show hidden files and folder is checked it will be hidden?

Want to hide some files and programming a program do it are different story.
A hide files software do it job by write some file system filter module (kernel or user space), or via api hooking(it OS spefify). If you just want to hide some files, use a software like http://www.wisecleaner.com/wisefolderhider.html on Windows. If you want to make it your own, you'll need more research on it.

Hide subfolders inside a folder in java

Java 7 solution

Path path = Paths.get("your/folder");
Files.setAttribute(path, "dos:hidden", true);

Or:

Path path = Paths.get("your/folder");
DosFileAttributeView attr = Files.getFileAttributeView(path, DosFileAttributeView.class);
attr.setHidden(true);

Why does Files.isHidden(Path) return false for directories on Windows?

I checked documentation for file attributes provided by Microsoft for Windows platform. It says that if attribute FILE_ATTRIBUTE_HIDDEN = 2 (0x2) is set

The file or directory is hidden. It is not included in an ordinary directory listing.

As I can see in the class sun.nio.fs.WindowsConstants there is the same value definition used by DosFileAttributes.isHidden() method - public static final int FILE_ATTRIBUTE_HIDDEN = 0x00000002; which for my understanding should be mapped one to one with the attribute available for Windows, so in general hidden flag for a directory should be working in the same way as for a regular file.
In relation to operating system/file system integration, this behaviour seems to be incorrect.

How do you filter hidden files using DirectoryStream.Filter

It's not a bug but a feature(!) known since jdk7, Windows hidden directory are not detected as hidden, see this bug and this one (fix jdk13).

As a workaround, you can do this :

import java.nio.file.attribute.DosFileAttributes;
...
DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
{
@Override
public boolean accept(Path entry) throws IOException
{
DosFileAttributes attr = Files.readAttributes(entry, DosFileAttributes.class);
return !attr.isHidden();
}
};


Related Topics



Leave a reply



Submit