Home > Java > javaTutorial > How to get the inode identifier of a file in java

How to get the inode identifier of a file in java

WBOY
Release: 2023-04-18 20:25:06
forward
659 people have browsed it

java gets the inode identifier of the file. If the file is deleted or renamed, the inode value will change. Therefore, you can record the inode after loading the File for the first time, and then check the inode value to determine whether the file has been deleted. , rename or recreate, etc.

Method 1

import java.io.File;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
/**
 * Created by bruce on 2022/3/27 21:39
 */
public class FileInodeReaderTest {
    public static void main(String[] args) {
        File file = new File("/logs/csp/sentinel-block.log");
        try {
            BasicFileAttributeView basicview = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
            BasicFileAttributes attr = basicview.readAttributes();
            System.out.println("attr.fileKey():" + attr.fileKey()
                    + " attr.creationTime:" + attr.creationTime()
                    + " attr.lastModifiedTime:" + attr.lastModifiedTime());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
Copy after login

Method 2

import java.io.File;
import java.nio.file.Files;
/**
 * Created by bruce on 2022/3/27 21:39
 */
public class FileInodeReaderTest {
    public static void main(String[] args) {
        File file = new File("/logs/csp/sentinel-block.log");
        try {
            Object inode = Files.getAttribute(file.toPath(), "unix:ino");
            System.out.println("inode->" + inode);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
Copy after login

The above is the detailed content of How to get the inode identifier of a file in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template