首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用Java发现文件的创建时间?

要使用Java发现文件的创建时间,您可以使用java.nio.file.Filesjava.nio.file.attribute.BasicFileAttributes类。以下是一个简单的示例代码:

代码语言:java
复制
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

public class FileCreationTime {
    public static void main(String[] args) {
        try {
            Path path = Paths.get("path/to/your/file");
            BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
            FileTime creationTime = attributes.creationTime();
            System.out.println("File creation time: " + creationTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这段代码将打印出文件的创建时间。请确保将"path/to/your/file"替换为您要检查的文件的实际路径。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

SpringBoot中如何上传Upload

上传: 根据第3部分的helloworld例子,用那个项目做底子。pom.xml都不用改变。参考项目bootUpload1. static/index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> index1 <form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" />

<input type="submit" value="Submit" /> </form> </body> </html> package com.SpringbootMaven; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.servlet.http.HttpServletResponse; @Controller public class UploadController { private static String UPLOADED_FOLDER = "e://temp//"; @RequestMapping("/upload") public void singleFileUpload(@RequestParam("file") MultipartFile file,HttpServletResponse res) throws IOException { try { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); Files.write(path, bytes); System.out.println("马克-to-win@马克java社区 successfully"); } catch (IOException e) { e.printStackTrace(); } res.sendRedirect("index.html"); } }

00

Java NIO-13.NIO Path

Java Path接口是Java NIO2一部分,是对Java6 和Java7的更新。Java的Path接口在Java7中被添加到Java NIO中,位于java.nio.file包中,全路径名为是java.nio.file.Path。 Java Path接口代表文件系统中的一个路径。路径指向一个文件或者目录。路径可以使绝对的或相对的。绝对路径包含了从根目录开始,到指向文件或目录的全部路径,相对路径包含文件或目录相对于其他路径的路径。相对路径听起来有点复杂,别担心,在Java NIO Path教程中会对相对路径进行详细解释。 不要混淆了文件系统中的路径和操作系统中环境变量的Path路径。java.nio.file.Path实例和环境变量中的Path没有任何关系。 很多地方java.nio.file.Path接口和java.io.File类是类似的,但是有一些不同。在很多情况下,能用Path接口代替File类。

03
领券