Java 17 Recipes


-9. Querying (and Setting) File Metadata


Download 3.2 Mb.
Pdf ko'rish
bet161/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   157   158   159   160   161   162   163   164   ...   245
Bog'liq
Java 17 Recipes

8-9. Querying (and Setting) File Metadata
 Problem
You need to get information about a particular file, such as file size, whether it is a 
directory, and so on. Also, you might want to mark a file as archived in the Windows 
operating system or grant-specific POSIX file permissions in the *nix operating system.
 Solution
Using Java NIO.2, you can obtain any file information by simply invoking methods on the 
java.nio.file.Files utility class, passing the path you’d like to obtain the metadata. 
You can obtain attribute information by calling the Files.getFileAttributeView() 
method, passing the specific implementation for the attribute view that you would like to 
use. The following code demonstrates these techniques for obtaining metadata.
public class Ch_8_09_MetadataInfo {
private void start() {
Path path = FileSystems.getDefault().getPath("./file2.log");
try {
// General file attributes, supported by all Java systems
System.out.println("File Size:"+Files.size(path));
System.out.println("Is Directory:"+Files.isDirectory(path));
System.out.println("Is Regular File:"+Files.
isRegularFile(path));
System.out.println("Is Symbolic Link:"+Files.
isSymbolicLink(path));
System.out.println("Is Hidden:"+Files.isHidden(path));
System.out.println("Last Modified Time:"+Files.
getLastModifiedTime(path));
System.out.println("Owner:"+Files.getOwner(path));
ChApTer 8 InpuT And OuTpuT


318
// Specific attribute views.
DosFileAttributeView view = Files.getFileAttributeView(path,
DosFileAttributeView.class);
System.out.println("DOS File Attributes\n");
System.out.println("------------------------------------\n");
System.out.println("Archive :"+view.readAttributes().
isArchive());
System.out.println("Hidden :"+view.readAttributes().
isHidden());
System.out.println("Read-only:"+view.readAttributes().
isReadOnly());
System.out.println("System :"+view.readAttributes().
isSystem());
view.setArchive(false);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Ch_8_09_MetadataInfo info = new Ch_8_09_MetadataInfo();
info.start();
}
 How It Works
Java NIO.2 allows much more flexibility in getting and setting file attributes than older 
I/O techniques. NIO.2 abstracts the different operating system attributes into both 
a “common” and an “OS-specific” set of attributes. The standard attributes are the 
following.
• isDirectory is true if it’s a directory
• isRegularFile returns false if the file isn’t considered a regular 
file, the file doesn’t exist, or it can’t be determined whether it’s a 
regular file.
ChApTer 8 InpuT And OuTpuT


319
• isSymbolicLink is true if the link is symbolic (most prevalent in Unix 
systems).
• isHidden is true if the file is considered hidden in the 
operating system.
• LastModifiedTime is the time the file was last updated.
• Owner is the file’s owner per the operating system.
Also, NIO.2 allows entering the specific attributes of the underlying operating system. 
To do so, you first need to get a view representing the operating system’s file attributes (in 
this example, it is a DosFileAttributeView). Once you get the view, you can query and 
change the OS-specific attributes.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   157   158   159   160   161   162   163   164   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling