0

I use BasicFileAttributes to get the attributes in Java in Windows as below:

public class Test {
    public static void main(String[] args) throws Exception  {
        BasicFileAttributes attr = null;
        Path path = Paths.get("some","file.txt");
        attr = Files.readAttributes(path, BasicFileAttributes.class);
        System.out.println(attr);
    }
}

The output of the above is something like:

sun.nio.fs.WindowsFileAttributes@a1234567

Hence I understand that readAttributes is returning a WindowsFileAttributes object. Now I want to cast BasicFileAttributes into WindowsFileAttributes and use its methods. But I am unable to import and use WindowsFileAttributes. I get error like:

java: package sun.nio.fs is not visible
(package sun.nio.fs is declared in module java.base, which does not export it to the unnamed module)

How do I fix this error?

  • *"How do I fix this error?"* By not trying. `WindowsFileAttributes` is the internal implementation of the `BasicFileAttributes` interface, and you shouldn't know about it. Why are you even trying? Just call the methods on [`BasicFileAttributes`](https://docs.oracle.com/javase/9/docs/api/java/nio/file/attribute/BasicFileAttributes.html). Or cast to [`DosFileAttributes`](https://docs.oracle.com/javase/9/docs/api/java/nio/file/attribute/DosFileAttributes.html) if you need the extra DOS/Windows specific methods, since that's a public interface. – Andreas Jan 15 '18 at 07:08
  • 1
    WindowsFileAttributes has methods like fileIndexHigh() and fileIndexLow(). I need those to uniquely identify a file across renames and moves. Is there anyway to get the IndexHigh and IndexLow? – Rajini Ravula Jan 15 '18 at 07:12
  • Can I get WindowsFileAttributes by reflection? – Rajini Ravula Jan 15 '18 at 07:12
  • Then you're using flawed logic, because the [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363788(v=vs.85).aspx) says *"File IDs are not guaranteed to be unique over time, because file systems are free to reuse them. In some cases, the file ID for a file can change over time."* It also says *"[...] not guaranteed to be unique on ReFS"*. You should rethink your logic. – Andreas Jan 15 '18 at 07:16
  • My use case requires FileIDs to be unique and not change for a very short period of time, I am accommodating changes to FileID during that time which further logic. ReFS has 128 bit file ID which needs to be used; 64 bit is not unique. – Rajini Ravula Jan 15 '18 at 07:32

1 Answers1

1

Classes within sun.* cannot be used.

One method is to use JNA to access native Windows functions as explained here: Get unique file id in Windows with Java?