JDK-8170334 : Files.isHidden() fails for folders on Windows
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 8,9
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: generic
  • Submitted: 2016-11-23
  • Updated: 2018-12-20
  • Resolved: 2016-11-25
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) Client VM (build 25.102-b14, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

EXTRA RELEVANT SYSTEM CONFIGURATION :
32 bit

A DESCRIPTION OF THE PROBLEM :
If you use java.nio.file.Files.isHidden(hiddenDirectory) on a hidden folder on windows (I used windows 7 - 32bit) it reports it as not hidden. Instead you had to use java.nio.file.Files.readAttributes(hiddenDirectory, DosFileAttributes.class).isHidden()

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
create a hidden folder on windows
execute Files.isHidden() on that folder
see that it returns false

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Files.isHidden(hiddenDirectory) should return true!
ACTUAL -
Files.isHidden(hiddenDirectory) returns false

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class FilesDotIsHiddenForWindowsFolder {

  public static void main(String[] args) throws IOException {
    Path hiddenDirectory = Paths.get("someHiddenDir");
    if(!Files.exists(hiddenDirectory)){
      Files.createDirectory(hiddenDirectory);
      assert Files.exists(hiddenDirectory) == true : "Something went wrong creating the test folder";
    }
    
    Files.setAttribute(hiddenDirectory, "dos:hidden", Boolean.TRUE);
    assert Files.readAttributes(hiddenDirectory, DosFileAttributes.class).isHidden() == true : "Something went wrong setting folder to be hidden";
    
    if(Files.isHidden(hiddenDirectory)){
      System.err.println("is hidden");
    }
    else{
      System.err.println("Folder is hidden, but Files.isHidden() returned false. Files.isHidden() should return true!");
    }
  }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
use Files.readAttributes(hiddenDirectory, DosFileAttributes.class).isHidden()


Comments
Per the javadocs for Files.isHidden: On Windows a file is considered hidden if it isn't a directory and the DOS hidden attribute is set.
25-11-2016

The javadoc for Files.isHidden says: On Windows a file is considered hidden if it isn't a directory and the DOS hidden attribute is set. As this is a directory, it would not be considered hidden.
25-11-2016

I don't think we have a bug here because the hidden attribute is meaningless on directories.
25-11-2016

Verified this issue against 8,8u112,9ea on Windows and could reproduce the issue as reported by the bug reporter. Steps to reproduce: ************************* - Run the attached test application(FilesDotIsHiddenForWindowsFolder.java) with JDK. Result: ********* OS : windows 7 64 bit JDK: 8 b32 : Fail 8u112 b14: Fail 8u122ea b02 : Fail 9ea+133 : Fail ================================================================================================================ Output: ********** C:\Users\ababroy\Desktop>javac FilesDotIsHiddenForWindowsFolder.java C:\Users\ababroy\Desktop>java FilesDotIsHiddenForWindowsFolder Folder is hidden, but Files.isHidden() returned false. Files.isHidden() should return true! C:\Users\ababroy\Desktop>
25-11-2016