Saturday 7 January 2023

Get file basic file attributes in Java

'BasicFileAttributes' interface define methods to get all the basic attributes associated with a file.

 

How to get BasicFileAttributes object of a file?

filePath
.getFileSystem()
.provider()
.getFileAttributeView(filePath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
.readAttributes();

 

Find the below working application.

 


BasicFileAttributesDemo.java

package com.sample.app.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Objects;

public class BasicFileAttributesDemo {

	public static final BasicFileAttributes getBasicAttributes(final String filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		return getBasicAttributes(new File(filePath));
	}

	public static final BasicFileAttributes getBasicAttributes(final File filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		return getBasicAttributes(filePath.toPath());

	}

	public static final BasicFileAttributes getBasicAttributes(final Path filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");

		return filePath.getFileSystem().provider()
				.getFileAttributeView(filePath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
				.readAttributes();
	}

	public static void main(String[] args) throws IOException {
		final String filePath = "/Users/Shared/filePrograms/a.txt";

		final BasicFileAttributes basicFileAttributes = getBasicAttributes(filePath);

		final FileTime creationTime = basicFileAttributes.creationTime();
		final Object fileKey = basicFileAttributes.fileKey();
		final boolean isDirectiory = basicFileAttributes.isDirectory();
		final boolean isRegularFile = basicFileAttributes.isRegularFile();
		final boolean isSymbolicLink = basicFileAttributes.isSymbolicLink();
		final FileTime lastAccessTime = basicFileAttributes.lastAccessTime();
		final FileTime lastModifiedTime = basicFileAttributes.lastModifiedTime();
		final long fileSizeInBytes = basicFileAttributes.size();

		System.out.println("creationTime : " + creationTime);
		System.out.println("fileKey : " + fileKey);
		System.out.println("isDirectiory : " + isDirectiory);
		System.out.println("isRegularFile : " + isRegularFile);
		System.out.println("isSymbolicLink : " + isSymbolicLink);
		System.out.println("lastAccessTime : " + lastAccessTime);
		System.out.println("lastModifiedTime : " + lastModifiedTime);
		System.out.println("fileSizeInBytes : " + fileSizeInBytes);

	}

}

Output

creationTime : 2023-01-07T15:41:28Z
fileKey : (dev=1000004,ino=37049869)
isDirectiory : false
isRegularFile : true
isSymbolicLink : false
lastAccessTime : 2023-01-07T15:41:30Z
lastModifiedTime : 2023-01-07T15:41:28Z
fileSizeInBytes : 12


 

 

You may like

file and stream programs in Java

Convert string to InputStream in Java

Read the data from BufferedReader in Java

Read file content using BufferedReader in Java

Touch the file in Java (Update the file timestamp)

Get POSIX file attributes in Java

No comments:

Post a Comment