File and Directory Attributes

Reading Time: 3 minutes

File and Directory Attributes

With these features Java allows us to read and modify file and folder attributes for all supported operating systems such as Windows and Unix families. There are a couple of ways to read and alter data by using Files and the regarding interface. If a file or folder does not support the regarding attribute view null will be returned.

Java Docs

Files: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

BasicFileAttributes: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html

BasicFileAttributeView: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributeView.html

DosFileAttribute: http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/DosFileAttributes.html

DosFileAttributeView: http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/DosFileAttributeView.html

PosixFileAttribute: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/PosixFileAttributes.html

PosixFileAttributeView: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/PosixFileAttributeView.html

AclFileAttributeView: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/AclFileAttributeView.html

AclEntry: https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/AclEntry.html

Global Attributes via Files class

Using Files class you can retrieve and set individual attributes of files and directories regardless of setting out the operation system.


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Map;

public class FilesAttributesSample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("data.txt");
// Retrieving some individual attributes
System.out.println("isExecutable: " + Files.isExecutable(path));
System.out.println("isWriteable: " + Files.isWritable(path));
System.out.println("isDirectory: " + Files.isDirectory(path));
System.out.println("isHidden: " + Files.isHidden(path));

// Setting some individual attributes
final long curr = System.currentTimeMillis();
FileTime creationFileTime = FileTime.fromMillis(curr);
Files.setAttribute(path, "lastAccessTime", creationFileTime);

// Getting all existing attributes
System.out.println("===All Attributes===");
Map<String, Object> allAttributes = Files.readAttributes(path, "*");
for (String attribute : allAttributes.keySet()) {
System.out.println(attribute + " : " + allAttributes.get(attribute));
}
}
}

OS Related Attribute Interfaces

These interfaces allows us to read and set attributes for a given specific given operating system. Attribute Interfaces help to read and View Interfaces help to set attributes.

Basic Attributes

This interface type of interface offers some basic methods for retrieval and manipulation data sets. While reading partial attributes given in the “optionalAttribute” map you can give the attributes you want to fetch. Keep in mind there is no free space between comas, otherwise java will throw a runtime exception


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Map;

public class BasicAttributesSample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("data.txt");

BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);

// Retrieving some individual attributes
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("lastAccessTime:" + attr.lastAccessTime());

final long curr = System.currentTimeMillis();
FileTime creationTime = FileTime.fromMillis(curr);
FileTime lastModifiedTime = FileTime.fromMillis(curr - 500000);
FileTime lastAccessedTime = FileTime.fromMillis(curr - 850000);
// Set times
view.setTimes(lastModifiedTime, lastAccessedTime, creationTime);

// Getting all existing attributes
System.out.println("===All Attributes===");
Map<String, Object> allAttribues = Files.readAttributes(path, "*");
Map<String, Object> optionalAttribues = Files.readAttributes(path, "creationTime,size");

// Iterate through attributes
for (String attribute : allAttribues.keySet())
System.out.println(attribute + " : " + allAttribues.get(attribute));
}
}

DOS Attributes

DOS Attributes are used in Windows Operating System. If you attempt to use this interface on a non-windows-system you’ll get a runtime exception. While getting attributes, and setting attributes via Files class, you are obligated to use “dos:” prefix at all times.


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.util.Map;

public class DOSAttributesSample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("data.txt");

DosFileAttributes attr = Files.readAttributes(path, DosFileAttributes.class);
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);

// Retrieving some individual attributes
System.out.println("isArchive: " + attr.isArchive());
System.out.println("isSystem: " + attr.isSystem());

// Setting attributes via DOS interface
view.setHidden(false);
view.setSystem(false);

// Setting attributes via Files class
Files.setAttribute(path, "dos:hidden", false);
Files.setAttribute(path, "dos:system", false);

// Getting all existing attributes
System.out.println("===All Attributes===");
Map<String, Object> allAttribues = Files.readAttributes(path, "dos:*");
Map<String, Object> optionalAttribues = Files.readAttributes(path, "dos:creationTime,size");

// Iterate through attributes
for (String attribute : allAttribues.keySet())
System.out.println(attribute + " : " + allAttribues.get(attribute));
}
}

POSIX Attributes

POSIX Attributes are used for UNIX systems.  If you attempt to use this interface on a non-unix-system you’ll get a runtime exception. While getting attributes, and setting attributes via Files class, you are obligated to use “posix:” prefix at all times.


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Map;

public class POSIXAttributesSample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("data.txt");

PosixFileAttributes attr = Files.readAttributes(path, PosixFileAttributes.class);
PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

// Retrieving some individual attributes
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("isOther : " + attr.isOther());

// Setting a permission attribute via POSIX interface
view.setPermissions(PosixFilePermissions.fromString("rwxr-x---"));

// Getting all existing attributes
System.out.println("===All Attributes===");
Map<String, Object> allAttribues = Files.readAttributes(path, "posix:*");
Map<String, Object> optionalAttribues = Files.readAttributes(path, "posix:creationTime,size");

// Iterate through attributes
for (String attribute : allAttribues.keySet())
System.out.println(attribute + " : " + allAttribues.get(attribute));
}
}

AclFileAttributeView Interface

This interface is utilized to read and update a given file’s ACL or file owner attributes. However, this interface is only used for Windows based operating systems. Please mind that the given user must exist on the system, otherwise UserPrincipalNotFoundException will be thrown. To see the current logged on user on command line type echo %username%


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.UserPrincipal;
import java.util.List;

public class ACLFileViewSample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("data.txt");

UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("Tugrul");

AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);

// grant "Tugrul" read access
AclEntry aclEntry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user)
.setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES).build();

//Get the owner name
System.out.println(view.getOwner().getName());

//Pring existing ACLs
List<AclEntry> aclData = view.getAcl();

//Setting a given ACL by the UserPrincipal
aclData.add(0, aclEntry);
view.setAcl(aclData);

for (AclEntry acl : aclData)
System.err.println(acl.toString() + "\n");

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *