Make a File/Folder Hidden on Windows with Java
Asked Answered
S

7

28

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?

Servais answered 18/8, 2009 at 16:25 Comment(1)
Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);, via javacodeexamples.com/make-file-hidden-in-java-example/1065Council
L
24

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.

Limpopo answered 18/8, 2009 at 16:29 Comment(2)
"native" means you're running platform specific code. exec() fires up a DOS/Windows shell to execute a DOS/Windows program.Heckle
what happens when this code is executed in Linux? Or how do I prevent it?Montparnasse
P
25

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.

Potsherd answered 18/8, 2009 at 19:15 Comment(0)
L
24

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.

Limpopo answered 18/8, 2009 at 16:29 Comment(2)
"native" means you're running platform specific code. exec() fires up a DOS/Windows shell to execute a DOS/Windows program.Heckle
what happens when this code is executed in Linux? Or how do I prevent it?Montparnasse
O
19

Java 7 can hide a DOS file this way:

Path path = Paths.get("...");
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);

Earlier Java versions don't have an API for this.

The above code will not throw an exception on non-DOS file systems. If the name of the file starts with a period, then it will be a hidden on UNIX file systems.

Overset answered 12/6, 2010 at 16:4 Comment(3)
The method getAttribute(String, LinkOption) is undefined for the type java.nio.file.Path (JDK 7u13)Hardwood
Antonio, it must have been that way in the draft version of Java 7 that I used. I see that similar functionality is now in java.nio.file.Files.Overset
You can use Files.setAttribute that will accept a Path to set the attribute on.Ammonia
C
3

this is what I use:

void hide(File src) throws InterruptedException, IOException {
    // win32 command line variant
    Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
    p.waitFor(); // p.waitFor() important, so that the file really appears as hidden immediately after function exit.
}
Charleton answered 19/8, 2009 at 7:14 Comment(0)
V
3

on windows, using java nio, Files

Path path = Paths.get(..); //< input target path
Files.write(path, data_byte, StandardOpenOption.CREATE_NEW); //< if file not exist, create 
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); //< set hidden attribute
Vouge answered 7/4, 2016 at 1:45 Comment(1)
Please add a description of how the code you posted addresses the user's questionHolliman
D
2

Here is a fully compilable Java 7 code sample which hides an arbitrary file on Windows.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;


class A { 
    public static void main(String[] args) throws Exception
    { 
       //locate the full path to the file e.g. c:\a\b\Log.txt
       Path p = Paths.get("c:\\a\\b\\Log.txt");

       //link file to DosFileAttributes
       DosFileAttributes dos = Files.readAttributes(p, DosFileAttributes.class);

       //hide the Log file
       Files.setAttribute(p, "dos:hidden", true);

       System.out.println(dos.isHidden());

    }
 } 

To check the file is hidden. Right-click on the file in question and you will see after the execution of the court that the file in question is truly hidden.

enter image description here

Deltoro answered 15/3, 2017 at 16:55 Comment(0)
O
0
String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);

Use this code it might solve you problem

Oribel answered 15/5, 2013 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.