파일 생성 날짜는 경로 에서 BasicFileAttributes 를 읽어 액세스 할 수 있습니다.
BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); FileTime time = attrs.creationTime();
다음은 Java에서 파일의 생성 날짜와 시간에 액세스하는 예입니다.
이 코드는 파일 경로에서 파일 객체를 만듭니다. Files.readAttributes 를 사용하여 Path 속성을 읽습니다. 생성 날짜 및 시간은 여기에서 액세스 할 수 있습니다. 시간은 FileTime 객체이며, 날짜로 변환되고 렌더링을 위해 포맷됩니다. 출력 문자열은 출력에 기록됩니다.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileCreationDate {
public static void main(String[] argv){
File file = new File( "V:/tmp/test.txt" );
BasicFileAttributes attrs;
try {
attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
FileTime time = attrs.creationTime();
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String formatted = simpleDateFormat.format( new Date( time.toMillis() ) );
System.out.println( "파일 생성 날짜 및 시간은 다음과 같습니다.: " + formatted );
} catch (IOException e) {
e.printStackTrace();
}
}
}
파일 생성 날짜 및 시간은 다음과 같습니다.: 2017-01-16 16:54:09