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中文网其他相关文章!