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
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
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!
Hi Jimmy,
Can you share the code for me to check?
Hi Wayan,
The code is identical to the source code above — just copied & pasted it into my file, DosFileAttributeExample.java.
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.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.
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!
Hi Jimmy,
Will this example help you to compile and run a Java class from command prompt: https://kodejava.org/how-do-i-write-hello-world-program-in-java/
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.
Hi Jimmy,
I’m still not sure what causes errors on your side, because it runs just fine when I try it.