POSIX stands for Portable Operating System Interface, it is a set of standard operating system interfaces based on the Unix operating system. POSIX standardizes set of guidelines to ease the task of cross-platform software development. You can refer this link for more information on POSIX.
How to get POSIX file attributes in Java?
Below snippet return the POSIX attributes in Java.
filePath .getFileSystem() .provider() .getFileAttributeView(filePath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS) .readAttributes();
Find the below working application.
PosixFileAttributesDemo.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.FileTime;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.Objects;
import java.util.Set;
public class PosixFileAttributesDemo {
public static final PosixFileAttributes getPosixAttributes(final String filePath) throws IOException {
Objects.requireNonNull(filePath, "filePath is null");
return getPosixAttributes(new File(filePath));
}
public static final PosixFileAttributes getPosixAttributes(final File filePath) throws IOException {
Objects.requireNonNull(filePath, "filePath is null");
return getPosixAttributes(filePath.toPath());
}
public static final PosixFileAttributes getPosixAttributes(final Path filePath) throws IOException {
Objects.requireNonNull(filePath, "filePath is null");
return filePath.getFileSystem().provider()
.getFileAttributeView(filePath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
.readAttributes();
}
public static void main(String[] args) throws IOException {
final String filePath = "/Users/Shared/filePrograms/a.txt";
final PosixFileAttributes posixFileAttributes = getPosixAttributes(filePath);
final FileTime creationTime = posixFileAttributes.creationTime();
final Object fileKey = posixFileAttributes.fileKey();
final boolean isDirectiory = posixFileAttributes.isDirectory();
final boolean isRegularFile = posixFileAttributes.isRegularFile();
final boolean isSymbolicLink = posixFileAttributes.isSymbolicLink();
final FileTime lastAccessTime = posixFileAttributes.lastAccessTime();
final FileTime lastModifiedTime = posixFileAttributes.lastModifiedTime();
final UserPrincipal owner = posixFileAttributes.owner();
final Set<PosixFilePermission> permissions = posixFileAttributes.permissions();
final long fileSizeInBytes = posixFileAttributes.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("owner : " + owner);
System.out.println("fileSizeInBytes : " + fileSizeInBytes);
System.out.println("File permissions : ");
for (final PosixFilePermission permission : permissions) {
System.out.println(" " + permission);
}
}
}
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 owner : krishna fileSizeInBytes : 12 File permissions : OWNER_WRITE OTHERS_READ OWNER_READ GROUP_READ
You may like
file and stream programs in Java
How to get the directory size in Java?
Convert string to InputStream in Java
Read the data from BufferedReader in Java
No comments:
Post a Comment