Home » Java programs » Java file handling programs

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


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

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.