Java Linux文件属性、lastModifiedTime、lastAccessTime、creationTime

Java Linux文件属性、lastModifiedTime、lastAccessTime、creationTime,java,linux,file-io,Java,Linux,File Io,我对Linux(Ubuntu)和文件属性有问题。我需要获取一些关于文件的数据,我需要的最重要的数据是lastModifiedTime,lastAccessTime,creationTime。在这里,我使用stat exception/EmptyAttributesListException.java命令,获取有关此文件的数据。问题是我的所有文件都具有相同的lastModifiedTime和creationTime。我想是关于Ubuntu的 File: exception/EmptyAttri

我对Linux(Ubuntu)和文件属性有问题。我需要获取一些关于文件的数据,我需要的最重要的数据是
lastModifiedTime
lastAccessTime
creationTime
。在这里,我使用
stat exception/EmptyAttributesListException.java
命令,获取有关此文件的数据。问题是我的所有文件都具有相同的lastModifiedTime和creationTime。我想是关于Ubuntu的

  File: exception/EmptyAttributesListException.java
  Size: 243         Blocks: 8          IO Block: 4096   regular file
Device: 807h/2055d  Inode: 4196285     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/  denisu)   Gid: ( 1000/  denisu)
Access: 2020-10-07 23:18:00.011710418 +0300
Modify: 2020-10-07 23:17:58.795607161 +0300
Change: 2020-10-07 23:17:58.795607161 +0300
 Birth: -
这也是我用来获取文件属性的java代码

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.UserPrincipal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Optional;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public final class FileAttribute {

    private Path basePath;
    private LocalDateTime lastModifiedTime;
    private LocalDateTime lastAccessTime;
    private LocalDateTime creationTime;
    private Long size;
    private Boolean isRegularFile;
    private Boolean isDirectory;
    private Boolean isSymbolicLink;
    private Boolean isOther;
    private UserPrincipal owner;
    private Optional<Path> relativePath;
    private long lines;

    public FileAttribute(Path filePath) throws IOException {
        BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
        this.basePath = filePath;

        this.creationTime = LocalDateTime.ofInstant(
                attr.creationTime().toInstant(),
                ZoneId.systemDefault());

        this.lastAccessTime = LocalDateTime.ofInstant(
                attr.lastAccessTime().toInstant(),
                ZoneId.systemDefault());

        this.lastModifiedTime = LocalDateTime.ofInstant(
                attr.lastModifiedTime().toInstant(),
                ZoneId.systemDefault());

        System.out.println(filePath);
        System.out.println("Creation: " + attr.creationTime());
        System.out.println("Access: " + attr.lastAccessTime());
        System.out.println("Modified: " + attr.lastModifiedTime());

        System.out.println("Creation: " + this.creationTime);
        System.out.println("Access: " + this.lastAccessTime);
        System.out.println("Modified: " + this.lastModifiedTime);

        System.out.println(this.creationTime.equals(this.lastModifiedTime));
        System.out.println(attr.creationTime().equals(attr.lastModifiedTime()) + "\n");

        this.isDirectory = attr.isDirectory();
        this.isOther = attr.isOther();
        this.isRegularFile = attr.isRegularFile();
        this.isSymbolicLink = attr.isSymbolicLink();
        this.size = attr.size();
        this.owner = Files.getOwner(filePath);
        this.relativePath = Optional.empty();
        this.lines = countLines(this.basePath);
    }

    public FileAttribute(Path filePath, Path fileRelativePath) throws IOException {
        BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
        this.basePath = filePath;

        this.creationTime = LocalDateTime.ofInstant(
                attr.creationTime().toInstant(),
                ZoneId.systemDefault());

        this.lastAccessTime = LocalDateTime.ofInstant(
                attr.lastAccessTime().toInstant(),
                ZoneId.systemDefault());

        this.lastModifiedTime = LocalDateTime.ofInstant(
                attr.lastModifiedTime().toInstant(),
                ZoneId.systemDefault());

        System.out.println(filePath);
        System.out.println("Creation: " + attr.creationTime());
        System.out.println("Access: " + attr.lastAccessTime());
        System.out.println("Modified: " + attr.lastModifiedTime());

        System.out.println("Creation: " + this.creationTime);
        System.out.println("Access: " + this.lastAccessTime);
        System.out.println("Modified: " + this.lastModifiedTime);

        System.out.println(this.creationTime.equals(this.lastModifiedTime));
        System.out.println(attr.creationTime().equals(attr.lastModifiedTime()) + "\n");

        this.isDirectory = attr.isDirectory();
        this.isOther = attr.isOther();
        this.isRegularFile = attr.isRegularFile();
        this.isSymbolicLink = attr.isSymbolicLink();
        this.size = attr.size();
        this.owner = Files.getOwner(filePath);
        this.relativePath = Optional.of(fileRelativePath);
        this.lines = countLines(this.basePath);
    }
}


我知道这些文件不是在同一时间创建和修改的,因为它们是我项目的一部分,我一直在添加代码。

我的理解是,虽然ext4能够存储创建时间(“出生”时间),但实际上没有内核设置它。注意您的
stat
输出如何显示
Birth:-
。这意味着没有可用的创建时间。
/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/search/GeneticSearch.java
Creation: 2000-11-01T06:25:26Z
Access: 2020-10-07T20:14:38.968798Z
Modified: 2000-11-01T06:25:26Z
Creation: 2000-11-01T08:25:26
Access: 2020-10-07T23:14:38.968798
Modified: 2000-11-01T08:25:26
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/New Text Document.txt
Creation: 2020-06-22T18:21:35.337793Z
Access: 2020-10-07T20:14:38.968798Z
Modified: 2020-06-22T18:21:35.337793Z
Creation: 2020-06-22T21:21:35.337793
Access: 2020-10-07T23:14:38.968798
Modified: 2020-06-22T21:21:35.337793
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/info_MS.txt
Creation: 2020-09-28T15:02:12.34448Z
Access: 2020-10-07T20:14:38.968798Z
Modified: 2020-09-28T15:02:12.34448Z
Creation: 2020-09-28T18:02:12.344480
Access: 2020-10-07T23:14:38.968798
Modified: 2020-09-28T18:02:12.344480
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Untitled.png
Creation: 2020-09-18T13:24:54.334452Z
Access: 2020-10-07T20:14:38.968798Z
Modified: 2020-09-18T13:24:54.334452Z
Creation: 2020-09-18T16:24:54.334452
Access: 2020-10-07T23:14:38.968798
Modified: 2020-09-18T16:24:54.334452
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/kindpng_3487784.png
Creation: 2020-09-26T14:59:46.235839Z
Access: 2020-10-07T20:14:38.972799Z
Modified: 2020-09-26T14:59:46.235839Z
Creation: 2020-09-26T17:59:46.235839
Access: 2020-10-07T23:14:38.972799
Modified: 2020-09-26T17:59:46.235839
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Takeoff/alg2.png
Creation: 2020-06-13T21:09:05.423772Z
Access: 2020-10-07T20:14:38.972799Z
Modified: 2020-06-13T21:09:05.423772Z
Creation: 2020-06-14T00:09:05.423772
Access: 2020-10-07T23:14:38.972799
Modified: 2020-06-14T00:09:05.423772
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Takeoff/Alg1.java
Creation: 2020-06-13T18:55:25.044948Z
Access: 2020-10-07T20:14:38.972799Z
Modified: 2020-06-13T18:55:25.044948Z
Creation: 2020-06-13T21:55:25.044948
Access: 2020-10-07T23:14:38.972799
Modified: 2020-06-13T21:55:25.044948
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Takeoff/Alg2.java
Creation: 2020-06-13T21:07:25.467566Z
Access: 2020-10-07T20:14:38.972799Z
Modified: 2020-06-13T21:07:25.467566Z
Creation: 2020-06-14T00:07:25.467566
Access: 2020-10-07T23:14:38.972799
Modified: 2020-06-14T00:07:25.467566
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Takeoff/alg1.png
Creation: 2020-06-13T21:08:27.206818Z
Access: 2020-10-07T20:14:38.972799Z
Modified: 2020-06-13T21:08:27.206818Z
Creation: 2020-06-14T00:08:27.206818
Access: 2020-10-07T23:14:38.972799
Modified: 2020-06-14T00:08:27.206818
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/07_NG_FORMS_ADVANCED.pdf
Creation: 2020-05-14T13:01:24.119627Z
Access: 2020-10-07T20:14:38.980799Z
Modified: 2020-05-14T13:01:24.119627Z
Creation: 2020-05-14T16:01:24.119627
Access: 2020-10-07T23:14:38.980799
Modified: 2020-05-14T16:01:24.119627
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/eula.1040.txt
Creation: 2007-11-07T05:00:40Z
Access: 2020-10-07T20:14:39.012801Z
Modified: 2007-11-07T05:00:40Z
Creation: 2007-11-07T07:00:40
Access: 2020-10-07T23:14:39.012801
Modified: 2007-11-07T07:00:40
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/Anexa_7_Reg_org_desf_practica_in_UPT(1).pdf
Creation: 2020-09-17T11:03:59.8919Z
Access: 2020-10-07T20:14:39.020801Z
Modified: 2020-09-17T11:03:59.8919Z
Creation: 2020-09-17T14:03:59.891900
Access: 2020-10-07T23:14:39.020801
Modified: 2020-09-17T14:03:59.891900
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/eula.1031.txt
Creation: 2007-11-07T05:00:40Z
Access: 2020-10-07T20:14:39.036802Z
Modified: 2007-11-07T05:00:40Z
Creation: 2007-11-07T07:00:40
Access: 2020-10-07T23:14:39.036802
Modified: 2007-11-07T07:00:40
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/Licenta/RandomFiles/readme.txt
Creation: 2020-05-23T17:38:26.549443Z
Access: 2020-10-07T20:14:39.036802Z
Modified: 2020-05-23T17:38:26.549443Z
Creation: 2020-05-23T20:38:26.549443
Access: 2020-10-07T23:14:39.036802
Modified: 2020-05-23T20:38:26.549443
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/target
Creation: 2020-08-21T15:31:23.554562Z
Access: 2020-10-07T20:14:39.036802Z
Modified: 2020-08-21T15:31:23.554562Z
Creation: 2020-08-21T18:31:23.554562
Access: 2020-10-07T23:14:39.036802
Modified: 2020-08-21T18:31:23.554562
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/example/some1
Creation: 2020-10-07T20:22:44.097121Z
Access: 2020-10-07T20:23:00.938666Z
Modified: 2020-10-07T20:22:44.097121Z
Creation: 2020-10-07T23:22:44.097121
Access: 2020-10-07T23:23:00.938666
Modified: 2020-10-07T23:22:44.097121
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/example/some2
Creation: 2020-10-07T20:22:54.058034Z
Access: 2020-10-07T20:23:00.962668Z
Modified: 2020-10-07T20:22:54.058034Z
Creation: 2020-10-07T23:22:54.058034
Access: 2020-10-07T23:23:00.962668
Modified: 2020-10-07T23:22:54.058034
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/resources/.gitignore-java-exemple
Creation: 2020-07-26T16:09:12.73309Z
Access: 2020-10-07T20:14:39.036802Z
Modified: 2020-07-26T16:09:12.73309Z
Creation: 2020-07-26T19:09:12.733090
Access: 2020-10-07T23:14:39.036802
Modified: 2020-07-26T19:09:12.733090
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/java/com/denisfeier/entity/FileAttribute.java
Creation: 2020-10-07T20:41:48.297447Z
Access: 2020-10-07T20:41:49.369427Z
Modified: 2020-10-07T20:41:48.297447Z
Creation: 2020-10-07T23:41:48.297447
Access: 2020-10-07T23:41:49.369427
Modified: 2020-10-07T23:41:48.297447
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/java/com/denisfeier/entity/GitCommit.java
Creation: 2020-10-07T20:33:40.23475Z
Access: 2020-10-07T20:33:41.374714Z
Modified: 2020-10-07T20:33:40.23475Z
Creation: 2020-10-07T23:33:40.234750
Access: 2020-10-07T23:33:41.374714
Modified: 2020-10-07T23:33:40.234750
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/java/com/denisfeier/GitLogGetPaths.java
Creation: 2020-10-07T20:38:49.703479Z
Access: 2020-10-07T20:38:50.955476Z
Modified: 2020-10-07T20:38:49.703479Z
Creation: 2020-10-07T23:38:49.703479
Access: 2020-10-07T23:38:50.955476
Modified: 2020-10-07T23:38:49.703479
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/java/com/denisfeier/scanner/RecursiveScanner.java
Creation: 2020-08-30T19:50:03.956808Z
Access: 2020-10-07T20:25:05.080017Z
Modified: 2020-08-30T19:50:03.956808Z
Creation: 2020-08-30T22:50:03.956808
Access: 2020-10-07T23:25:05.080017
Modified: 2020-08-30T22:50:03.956808
true
true

/home/denisu/IdeaProjects/GitLogGenerator/src/main/java/com/denisfeier/gitLogBuilder/LogBuilder.java
Creation: 2020-10-07T20:30:29.149667Z
Access: 2020-10-07T20:30:30.265678Z
Modified: 2020-10-07T20:30:29.149667Z
Creation: 2020-10-07T23:30:29.149667
Access: 2020-10-07T23:30:30.265678
Modified: 2020-10-07T23:30:29.149667
true
true