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

In this java program, we are going to learn how to get DOS based basic attributes like file’s hidden, read-only property etc?
Submitted by IncludeHelp, on November 07, 2017

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.

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());
  }
}

Output

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

Java File Handling Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.