java取得檔案的inode標識符,如果檔案被刪除或重新命名,inode的值會發生變更,因此可以在第一次載入File之後記錄inode,後續校驗inode的值來判斷檔案是否被刪除、重新命名或重新建立等。
方法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()); } } }
方法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()); } } }
以上是java怎麼取得文件的inode標識符的詳細內容。更多資訊請關注PHP中文網其他相關文章!