Write a java program to Get the basic file attributes


This is a Java program that reads and displays the DOS attributes of a file specified by the user. It uses the java.nio.file package to work with files and file attributes.

The program first prompts the user to enter the file path of the file whose attributes need to be checked. Then, it creates a Path object using the entered file path.

Next, it creates an object of the DosFileAttributeView class using the Files.getFileAttributeView() method. This object allows us to view the DOS attributes of the file.

Then, the program reads the attributes of the file using the view.readAttributes() method, which returns an object of the DosFileAttributes class.

Finally, the program displays the DOS attributes of the file using the isArchive(), isHidden(), isReadOnly(), and isSystem() methods of the DosFileAttributes class. The isArchive() method returns true if the file has the archive attribute set, isHidden() returns true if the file is hidden, isReadOnly() returns true if the file is read-only, and isSystem() returns true if the file is a system file.

Source Code

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Scanner;
public class DosAttribute_File
{
	public static void main(String[] args) throws Exception
	{	
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the File Path : ");
		String f = input.next();
		Path f_path = FileSystems.getDefault().getPath(f);
 
		// create object of dos attribute.
		DosFileAttributeView view = Files.getFileAttributeView(f_path, DosFileAttributeView.class);
 
		//Read the attribute of dos file.
		DosFileAttributes attr = view.readAttributes();
 
		System.out.println("isArchive : " + attr.isArchive());
		System.out.println("isHidden : " + attr.isHidden());
		System.out.println("isReadOnly : " + attr.isReadOnly());
		System.out.println("isSystem : " + attr.isSystem());
	}
}

Output