Java program to get the basic file attributes (specific to DOS)


Given a file and we have to find its attributes using java program.

Following packages are using here, to implement this program,

  • java.nio.file.*
  • java.nio.file.attribute.*

There are following two important classes, which are using this program to get the DOS based file attributes

  • DosFileAttributeView
  • DosFileAttributes

The method readAttributes() of DosFileAttributeView is using to is using to get the Basic file attributes.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to get DOS based basic file attributes in java

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Scanner;

public class DosAttributeOfFile {
  public static void main(String[] args) throws Exception {
    // create object of scanner.
    Scanner KB = new Scanner(System.in);

    // enter path here.
    System.out.print("Enter the file path : ");
    String A = KB.next();
    Path path = FileSystems.getDefault().getPath(A);

    // create object of dos attribute.
    DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);

    // this function read the attribute of dos file.
    DosFileAttributes attributes = view.readAttributes();

    System.out.println("isArchive: " + attributes.isArchive());
    System.out.println("isHidden: " + attributes.isHidden());
    System.out.println("isReadOnly: " + attributes.isReadOnly());
    System.out.println("isSystem: " + attributes.isSystem());


First run:
Enter the file path : E:/JAVA
isArchive: false
isHidden: true
isReadOnly: false
isSystem: false

Second run:
Enter the file path : D:/Study
isArchive: false
isHidden: false
isReadOnly: false
isSystem: false


need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions

need a help?

find thousands of online teachers now