zoukankan      html  css  js  c++  java
  • Java获得文件的创建时间(精确到秒)

    jni C/C++ 头文件:MyFileTime.h

    C/C++ code

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class MyFileTime */

    #ifndef _Included_MyFileTime
    #define _Included_MyFileTime
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class: MyFileTime
    * Method: getFileCreationTime
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
    JNIEXPORT jstring JNICALL Java_MyFileTime_getFileCreationTime
    (JNIEnv *, jclass, jstring);

    #ifdef __cplusplus
    }
    #endif
    #endif

    -------------------------------------------------------------------------------

    源文件:MyFileTime.c

    C/C++ code

    #include <windows.h>
    #include "MyFileTime.h"

    JNIEXPORT jstring JNICALL Java_MyFileTime_getFileCreationTime(JNIEnv *env, jclass cls, jstring FileName)
    {
    HANDLE hFile;
    FILETIME creationTime;
    FILETIME lastAccessTime;
    FILETIME lastWriteTime;
    FILETIME creationLocalTime;
    SYSTEMTIME creationSystemTime;
    jstring result;
    char fileTimeString[30];

    hFile = CreateFile((char *)env->GetStringUTFChars(FileName, 0), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if(hFile == INVALID_HANDLE_VALUE) return env->NewStringUTF("");
    if(GetFileTime(hFile, &creationTime, &lastAccessTime, &lastWriteTime))
    {
    if(FileTimeToLocalFileTime(&creationTime, &creationLocalTime))
    {
    if(FileTimeToSystemTime(&creationLocalTime, &creationSystemTime))
    {
    sprintf(fileTimeString,
    "%d.%d.%d %d:%d:%d.%d",
    creationSystemTime.wYear,
    creationSystemTime.wMonth,
    creationSystemTime.wDay,
    creationSystemTime.wHour,
    creationSystemTime.wMinute,
    creationSystemTime.wSecond,
    creationSystemTime.wMilliseconds);
    result = env->NewStringUTF(fileTimeString);
    }
    else
    result = env->NewStringUTF("");
    }
    else
    result = env->NewStringUTF("");
    }
    else
    result = env->NewStringUTF("");
    CloseHandle(hFile);
    return result;
    }

    -----------------------------------------------------------------------------------

    精确到毫秒:

    public final class MyFileTime {
    static {
    System.loadLibrary("MyFileTime");
    }

    private static native String getFileCreationTime(String fileName);

    public static String getCreationTime(String fileName) {
    return getFileCreationTime(fileName);
    }

    public static void main(String[] args) {
    System.out.println(MyFileTime.getCreationTime("D:/modeldesign/MyStuff/MyFileTime.dll"));
    }
    }

    结果是字符串:
    2008.4.25 23:16:19.890(年.月.日 时:分:秒.毫秒)

    =======================================================

    其他方法  - --  仅供参考


    #include "stdio.h"
    #include "windows.h"
    void main(int argc,char** argv){

    HFILE hFile;
    OFSTRUCT lp;
    FILETIME creationTime;
    FILETIME lastAccessTime;
    FILETIME lastWriteTime;
    FILETIME creationLocalTime;
    SYSTEMTIME creationSystemTime;


    hFile = OpenFile(argv[1],&lp, OF_READ);
    if(hFile == HFILE_ERROR)
    {
    printf("");
    return;
    }
    if(GetFileTime((HANDLE)hFile, &creationTime, &lastAccessTime, &lastWriteTime))
    {
    if(FileTimeToLocalFileTime(&creationTime, &creationLocalTime))
    {
    if(FileTimeToSystemTime(&creationLocalTime, &creationSystemTime))
    {
    printf("%d.%d.%d-%d:%d:%d.%d",
    creationSystemTime.wYear,
    creationSystemTime.wMonth,
    creationSystemTime.wDay,
    creationSystemTime.wHour,
    creationSystemTime.wMinute,
    creationSystemTime.wSecond,
    creationSystemTime.wMilliseconds);
    return ;
    }
    }
    }
    printf("");
    return ;
    }

    然后,Process p = Runtime.getRuntime().exec("cmd /c c:\GetFileTime1.exe c:\t1.txt");中再读取吧。否则怎办呢?
    为方便你处理,将结果输出改为:2008.4.24-20:11:3.480 格式。

    另:dir命令显示的最后修改的时间,而不是创建文件的时间。

    以上仅供你参考。

  • 相关阅读:
    3.2.1 webpack-dev-server的使用
    打印预览及打印
    2.1.8 webpack的环境
    常用docker 启动
    github镜像
    Log解析
    【Bzoj4555】【Luogu P4091】求和(NTT)
    【Bzoj3527】【Luogu3338】[Zjoi2014]力(FFT)
    FFT实现高精度乘法
    Bzoj 2190 仪仗队(莫比乌斯反演)
  • 原文地址:https://www.cnblogs.com/yhtboke/p/5726500.html
Copyright © 2011-2022 走看看