The Java file class provides access to the properties of the file, but file provides limited functionality.
Java7 's nio.2 provides a large number of tool classes under the Java.nio.file.attribbute package, through which developers can read and modify file attributes very simply, which are divided into the following two categories.
Xxxattributeview: Represents a view of a file property.
Xxxattributes: Represents a collection of properties of a file that is generally obtained xxxattributes by a Xxxattributeview object.
Below I only introduce two kinds, actually still have many.
Basicfileattributeview: It can get or modify the basic properties of a file, including the last modified 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 to the basic properties of a folder is done through the Basicfileatributes object.
Dosfileattributeview: It is mainly used to get or modify file DOS related properties, such as whether the file is read-only, hidden, whether it is a system file, whether it is an archive file, etc. Its readattributes () method returns a Dosfileattributes object modification of these properties 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 voidMain (string[] args)throwsException
- {
- path path=paths.get (" d:/address.txt "
- basicfileattributeview basicview=files.getfileattributeview (Path, basicfileattributeview.class );
- basicfileattributes basicfile=basicview.readattributes ();
- system.out.println (" creation Time " + new date (Basicfile.creationtime (). Tomillis ()));
- system.out.println (" File Size " +basicfile.size ());
- dosfileattributeview dosview=files.getfileattributeview (path, Dosfileattributeview.class );
- dosview.sethidden (true );
- dosview.setreadonly (true );
- }
- }
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
Java Access file properties