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.example.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() = true
isSystem()   = false

2 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

Leave a Reply

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