Java-获取文件的创建日期

Java-获取文件的创建日期

Tags: Java

摘要

Java-获取文件的创建日期

windows下获取文件创建时间

通过命令cmd /c dir c:\logfile.log /tc可以查看logfile.log这个文件的创建时间。第六行即是。

代码

package cn.javacore.io.file;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class _15_GetFileCreationDateExample {

    //仅适用于windows下,因为linux下没有创建时间的概念。linux下只有最后访问时间,最后修改时间,最后读取时间
    //  Why doesn't Linux store file creation time  ?  http://www.answers.com/Q/Why_doesn%27t_Linux_store_file_creation_time

    public static void main(String[] args) {
        try {
            Process proc =
                    Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
            BufferedReader br =
                    new BufferedReader(
                            new InputStreamReader(proc.getInputStream()));

            String data = "";

            //it's quite stupid but work
            for (int i = 0; i < 6; i++) {
                data = br.readLine();
            }

            System.out.println("Extracted value : " + data);

            //split by space
            StringTokenizer st = new StringTokenizer(data);
            String date = st.nextToken();//Get date
            String time = st.nextToken();//Get time

            System.out.println("Creation Date  : " + date);
            System.out.println("Creation Time  : " + time);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }
}

输出

Extracted value : 2016-03-23  22:59               677 logfile.log
Creation Date  : 2016-03-23
Creation Time  : 22:59