已知文件名,路径,如何获得该文件的创建时间?file类的lastmodified方法是获得修改时间,不是创建时间,有没有什么好的方法,跨平台的那个方法太难了,有没有简单的呢?

解决方案 »

  1.   

    因为要保证跨平台性,所以没有创建时间这个方法,只能通过windows里面的函数去读取了
      

  2.   

    貌似只能用jni了
    http://topic.csdn.net/u/20080424/16/0a7b24d6-326d-44ab-9e1b-e7b5312d96ce.html
      

  3.   

    这个肯定要用到本地方法了,与操作系统有关。
    偶没用到过,帮LZ顶了。LZ找到方法别忘了分享下。
      

  4.   

    这是我们JAVA作业的一题,我上网查了下,还是不会,实在不行,因为时间问题也只好放弃。
      

  5.   

    使用jni调用C模块。现学现卖,用了4个小时。参考《java核心技术卷2》的有关章节,baidu搜索.
    1.建个java模块。
    public class FileCreateTime{
    static native String getCreateTime(String s);
    static {
    System.loadLibrary("FileCreateTime");}
    public static void main(String[] args){
    String s;
    s =FileCreateTime.getCreateTime(args[0]);
    System.out.println(args[0]+" Create time is:" +s);
    }}
    2.执行命令:javac FileCreateTime.java生成 FileCreateTime.class
    3.执行命令:javah FileCreateTime
    4.实现头文件声明的函数并制作动态链接库。
    我用微型编译器tinycc 0.9.25 for win
    为了减少命令行的长度,把java系统目录下的文件包括那个win32目录下的文件一个个复制到tinycc编译器的include目录下(不要复制win32,要复制其下面的文件,省得找不到)。改好的FileCreateTime.c如下:
    #include "FileCreateTime.h"
    #include <windows.h>
    #include <stdio.h>
    JNIEXPORT jstring JNICALL Java_FileCreateTime_getCreateTime
      (JNIEnv *env, jclass jc , jstring js){
      jstring jstr;
      char buf[100];
      const char * cformat;
      FILETIME t,t2,t3,lt;
    HANDLE f;
    SYSTEMTIME st;
    cformat = (*env)->GetStringUTFChars(env, js, NULL);
    f=CreateFile(cformat, GENERIC_READ,                   // open for reading
      0,                              // do not share
      NULL,                           // default security
      OPEN_EXISTING,                  // existing file only
      FILE_ATTRIBUTE_NORMAL,          // normal file
      NULL);                          // no template
     if (f == INVALID_HANDLE_VALUE)
     {
      //printf("Could not open file.");
      return NULL;
     } 
    GetFileTime(f, &t, &t2, &t3); //取得文件创建,修改,存取时间
    FileTimeToLocalFileTime(&t,&lt);//把创建时间从世界时转成本地时间。
    FileTimeToSystemTime(&lt,&st);//转成本地格式
      sprintf(buf,"%d-%d-%d %02d:%02d:%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);//转成字符串
    jstr = (*env)->NewStringUTF(env, buf);
    return jstr;
       
      }
    编译方式极为简单:tcc -shared FileCreateTime.c
    它会生成FileCreateTime.dll就能用了。
    5.试试FileCreateTime.c的创建时间执行:java FileCreateTime FileCreateTime.c