Java获得文件的创建时间(精确到秒)
jni C/C++ 头文件:MyFileTime.h
/* 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
#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\0",
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\0",
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命令显示的最后修改的时间,而不是创建文件的时间。
以上仅供你参考。