Java access file properties

http://blog.csdn.net/zhy_cheng/article/details/7897290

Java's File class provides attributes for accessing files, but the functions provided by File are limited.
NIO.2 of Java7 provides a large number of tool classes under the java.nio.file.attribbute package. Through these tool classes, developers can easily read and modify file attributes. These tool classes are mainly divided into the following two categories.
XxxAttributeView: A view representing a certain file attribute.
XxxAttributes: Represents a collection of file attributes. The program generally obtains XxxAttributes through the XxxAttributeView object.
I will only introduce two of them below, but there are many more.
BasicFileAttributeView: It can get or modify the basic attributes of the file, including the last modification time of the file, the last access time, the creation time, the size, whether it is a directory, whether it is a symbolic link, etc. Its readAttribute() method returns a BasicFileAttributes object, and the modification of the basic attributes of the folder is done through the BasicFileAtributes object.
DosFileAttributeView: It is mainly used to obtain or modify file DOS-related attributes, such as whether the file is read-only, whether it is hidden, whether it is a system file, whether it is an archive file, etc. Its readAttributes() method returns a DosFileAttributes object The modification of these attributes is actually done by the DosfileAttributes object.

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.DosFileAttributeView; 
import java.util.Date; 
public class Test { 
    public static void main(String[] args) throws Exception 
    { 
        Path path=Paths.get("D:/address.txt"); 
        BasicFileAttributeView basicview=Files.getFileAttributeView(path, BasicFileAttributeView.class); 
        BasicFileAttributes basicfile=basicview.readAttributes(); 
        System.out.println("创建时间"+new Date(basicfile.creationTime().toMillis())); 
        System.out.println("文件大小"+basicfile.size()); 
        DosFileAttributeView dosview=Files.getFileAttributeView(path, DosFileAttributeView.class); 
        dosview.setHidden(true); 
        dosview.setReadOnly(true); 
        } 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570050&siteId=291194637