How to get inode information for a file in Java

Source: Internet
Author: User

Students familiar with the Linux operating system should know that the Linux file system uses the Inode-block structure to represent and store files, the inode contains the metadata information of the file, block stores the actual file content. Linux abstracts all of the IO into files, so directories are files as well as ordinary files. Each file has a unique inode number.


You can use the Stat command to view inode information for a file, such as the following file, stat Log.txt

You can see that the device is 16777219d and the inode number is 1821216. The contents of this file are empty, so blocks is 0


Rename this log.txt to Log2.txt, and then perform stat log2.txt, and you can see that only the file name has changed, and the other inode numbers have not changed.



In fact, the INODE data structure of an ordinary file stores only some metadata and does not store the file name information. So where does the file name exist, and we know that the directory is also a file, then it also has inode and block. Block blocks of ordinary files store the contents of the file, the block block of the directory stores the inode number of all the files in the directory, and the corresponding file name, and other information.


So renaming a file only modifies the file name information in the directory block of the directory in which it resides, and does not make any changes to the inode structure of the files.


Since the inode is so useful that it can identify a unique file, how does Java get the inode information? Because you often encounter a scenario that determines whether two files are the same file.

Java NIO provides an basicfileattributes interface to obtain inode information for a file. It provides a filekey () method that returns an object that contains the DeviceID and inode numbers of a file, which uniquely identifies a file.


Import Java.nio.file.files;import Java.nio.file.path;import Java.nio.file.paths;import Java.nio.file.attribute.basicfileattributes;public class Inodeattributes {       public static void Main (string[] args) throws Exception {          path Path = Paths.get ("/xxxxx/log.txt2");          basicfileattributes BFA = Files.readattributes (Path, basicfileattributes.class);                      System.out.println ("Creation time      : "+ bfa.creationtime ());          System.out.println (" Last Access time   : "+ bfa.lastaccesstime ());          System.out.println (" Last Modified time: "+ bfa.lastmodifiedtime ());          System.out.println (" is Directory      : "+ bfa.isdirectory ());          System.out.println ("is other          :" + bfa.isother ());           System.out.println ("is Regular file   :" + Bfa.isregularfile ());          System.out.println ("is symbolic link   : "+ bfa.issymboliclink ());          System.out.println (" size               : "+ bfa.size ());          Object Objectkey = Bfa.filekey ();           SYSTEM.OUT.PRINTLN ("Object key               : "+ bfa.filekey ());               &NBSp }    }

See the DeviceID and inode numbers of the resulting output consistent with the results returned with the Linux Stat command

Creation time      : 2015-01-21t03:20:41zlast Access time   : 2015-01-21t03:38:01zlast Modified time:2015-01-21t03 : 20:41zis Directory       : Falseis other           : Falseis Regular File    : Trueis symbolic Link   : falsesize               : 0Obje CT Key               : (dev=1000003,ino=1821216)


How to get inode information for a file in Java

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.