Java Access file properties

Source: Internet
Author: User

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.

  1. Import  Java.nio.file.Files;
  2. import  java.nio.file.path;  
  3. import  java.nio.file.paths;  
  4. import  java.nio.file.attribute.basicfileattributeview;  
  5. import  java.nio.file.attribute.basicfileattributes;  
  6. import  java.nio.file.attribute.dosfileattributeview;  
  7. import  java.util.date;  
  8. Public class Test {
  9. Public Static voidMain (string[] args)throwsException
  10. {
  11.          path path=paths.get (" d:/address.txt "
  12.          basicfileattributeview basicview=files.getfileattributeview (Path,  basicfileattributeview.class );   
  13.          basicfileattributes basicfile=basicview.readattributes ();  
  14.          system.out.println (" creation Time " + new  date (Basicfile.creationtime (). Tomillis ()));   
  15.          system.out.println (" File Size " +basicfile.size ());  
  16.          dosfileattributeview dosview=files.getfileattributeview (path,  Dosfileattributeview.class );   
  17.          dosview.sethidden (true );   
  18.          dosview.setreadonly (true );   
  19. }
  20. }

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Java Access file properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.