'java create file'에 해당되는 글 1건

파일 생성 날짜


FileMain.java

public class FileMain {

    public static void main(String[] args) {
        FileUtil file = new FileUtil();
        
        file.fileCreatedInfo("./TestingFile.txt");
    }
}

FileUtil.java

/**
     * 파일 생성 날짜 알아오기
     * @param path - 파일 위치
     */
    public void fileCreatedInfo(String path) {
        File file = new File(path);
        String formatted = "";
        BasicFileAttributes attrs = null;

        try {
            attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
            FileTime time = attrs.creationTime();

            String pattern = "yyyy-MM-dd";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

            formatted = simpleDateFormat.format(new Date(time.toMillis()));
            
            System.out.println("생성날짜 : "+new Date(time.toMillis()));
            System.out.println("형식지정표현 생성날짜 : " + formatted);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

읽어올 파일

결과화면

블로그 이미지

미나미나미

,