Write a java program to Get the attributes of a file


These Java programs demonstrate different file-related operations, such as checking if a file exists and can be read, deleting a file, copying a file, getting the last modification time of a file, appending a string to a file, and getting file attributes such as creation time and size.

  • The first program, File_ReadNot, checks if a file can be read by creating a File object with the file path, then using the canRead method to check if the file is readable. If the file is readable, the program prints a message indicating so, otherwise, it prints a message indicating that the file is not readable.
  • The second program, Delete_File, deletes a file by creating a File object with the file name, checking if the file exists using the exists method, then deleting the file using the delete method. If the file is deleted successfully, the program prints a message indicating so, otherwise, it prints a message indicating that the file deletion failed.
  • The third program, Copy_File, copies the contents of one file to another file by creating FileInputStream and FileOutputStream objects with the input and output file paths, respectively, then using a while loop to read bytes from the input file and write them to the output file. If the copy is successful, the program prints a message indicating so, otherwise, it prints an exception message.
  • The fourth program, FileLast_Modify, gets the last modification time of a file by creating a File object with the file path, then using the lastModified method to get the time in milliseconds since the epoch. The program then converts this time to a human-readable date format using the Date class and prints the result.
  • The fifth program, FileAppend, appends a string to a file by creating a FileOutputStream object with the file path and the true flag to indicate that the file should be opened in append mode. The program then writes the string content to the file using the write method and closes the file. If the append is successful, the program prints a message indicating so, otherwise, it prints an exception message.
  • The sixth program, Owner_File, gets the owner of a file by creating a Path object with the file path, then creating a FileOwnerAttributeView object with the path and using the getOwner method to get the owner information as a UserPrincipal object. The program then prints the name of the owner.
  • The seventh program, File_Attribute, gets the file attributes of a file by creating a Path object with the file path, then creating a BasicFileAttributeView object with the path and using the readAttributes method to get a BasicFileAttributes object with the file attributes. The program then prints the creation time, last accessed time, last modified time, file key, whether the file is a directory or a regular file or a symbolic link or some other type of file, and the size of the file.

Source Code

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Scanner;
 
public class File_Attribute
{
	public static void main(String[] args) throws Exception
	{
		Scanner in = new Scanner(System.in);
		System.out.print("Enter the File Path : ");
		String f = in.next();
		Path file_path = FileSystems.getDefault().getPath(f);
 
		// function is used to view file attribute
		BasicFileAttributeView view = Files.getFileAttributeView(file_path, BasicFileAttributeView.class);
		BasicFileAttributes attr = view.readAttributes();
 
		// attributes of the file
		System.out.println("Creation Time : " + attr.creationTime());
		System.out.println("Last Accessed Time : " + attr.lastAccessTime());
		System.out.println("Last Modified Time : " + attr.lastModifiedTime());
		System.out.println("File Key : " + attr.fileKey());
		System.out.println("Directory : " + attr.isDirectory());
		System.out.println("Other Type of File : " + attr.isOther());
		System.out.println("Regular File : " + attr.isRegularFile());
		System.out.println("Symbolic File : " + attr.isSymbolicLink());
		System.out.println("Size : " + attr.size());
	}
}

Output