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()); } } }
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()); } } }
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!