How do I use the DosFileAttributes class?

This example show you how to use the DosFileAttributes class to get file attribute that support DOS file system. This class extends the BasicFileAttributes class. Using the DosFileAttributes class we can read file attributes using isArchive(), isHidden(), isReadOnly() and isSystem() methods.

Let’s see the code snippet below:

package org.kodejava.io;

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

public class DosFileAttributeExample {
    public static void main(String[] args) throws Exception {
        String path = "D:/resources/data.txt";

        Path file = Paths.get(path);
        DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class);

        System.out.println("isArchive()  = " + attr.isArchive());
        System.out.println("isHidden()   = " + attr.isHidden());
        System.out.println("isReadOnly() = " + attr.isReadOnly());
        System.out.println("isSystem()   = " + attr.isSystem());
    }
}

The output of the code snippet:

isArchive()  = true
isHidden()   = false
isReadOnly() = false
isSystem()   = false
Wayan

8 Comments

  1. Hi, Wayan. Thanks for this example of how to use the DosFileAttributes class to get such file attributes as Archive and System.

    I tried compiling the example above (I’m using Java 1.7, the version that introduced this class), but am getting the following two compiler error messages:

    (1) Semantic Error: No applicable overload for a method with signature “get(java.lang.String)” was found in type “java.nio.file.Paths”. Perhaps you wanted the overloaded version “java.nio.file.Path get(java.net.URI $1);” instead?

    (2) Semantic Error: No applicable overload for a method with signature “readAttributes(java.nio.file.Path, java.lang.Class)” was found in type “java.nio.file.Files”. Perhaps you wanted the overloaded version “java.util.Map readAttributes(java.nio.file.Path $1, java.lang.String $2, java.nio.file.LinkOption… $3) throws java.io.IOException;” instead.

    Do you happen to know what can be done to fix this?

    Thanks again!

    Reply
  2. Hi Wayan,

    The code is identical to the source code above — just copied & pasted it into my file, DosFileAttributeExample.java.

    Reply
    • Hi Jimmy,

      Did you create the directories for the package? And how did you run the code? Using and IDE or from command prompt? And also can you inform the Java version by running the java -version command.

      Reply
  3. Hi again, Wayan!

    Did you create the directories for the package?

    No. Did I miss instructions on the kodejava.org page somewhere that says how to obtain & install the org.kodejava.example.io files? (I looked but could not find such a page).

    And how did you run the code? Using and IDE or from command prompt?

    From the command prompt in a command window.

    And also can you inform the Java version by running the java -version command.

    java version "1.7.0_80"
    Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
    

    I look forward to getting your example working, as I see many very useful examples on this website. I think that, once you have a good basis of a computer language, that one of the best ways of learning is by example.

    Thank you again for your time, Wayan!

    Reply
  4. Hi Wayan,

    The Hello, world program compiled & ran fine.

    The DosFileAttributes example code is identical to the above, but it still results in the same two compiler errors:

    (1) Semantic Error: No applicable overload for a method with signature “get(java.lang.String)” was found in type “java.nio.file.Paths”. Perhaps you wanted the overloaded version “java.nio.file.Path get(java.net.URI $1);” instead?

    (2) Semantic Error: No applicable overload for a method with signature “readAttributes(java.nio.file.Path, java.lang.Class)” was found in type “java.nio.file.Files”. Perhaps you wanted the overloaded version “java.util.Map readAttributes(java.nio.file.Path $1, java.lang.String $2, java.nio.file.LinkOption… $3) throws java.io.IOException;” instead.

    I have compiled and run other Java code on my machine, so it seems to be something with the get() and readAttributes() method calls.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.