Home >Java >javaTutorial >How Can I Retrieve File Creation Dates in Java?

How Can I Retrieve File Creation Dates in Java?

DDD
DDDOriginal
2024-11-02 10:37:301058browse

How Can I Retrieve File Creation Dates in Java?

Retrieving File Creation Date in Java

Determining the creation date of a file is crucial for tasks like organizing files by age. This article addresses a common question: "Is there a way to retrieve file creation dates in Java?"

According to the Java documentation, it's possible to access file metadata, including creation time, using Java's New I/O (NIO) library. Here's an example code snippet:

<code class="java">Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());</code>

This code retrieves the creation time attribute of a file and prints it to the console.

Note that the availability of file creation time may vary depending on the underlying file system. It's recommended to check if the file system supports creation time metadata before attempting to access it.

The above is the detailed content of How Can I Retrieve File Creation Dates in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn