最近的项目中遇到一个需求,要求判断文件是否被修改。这下犯了难。一开始打算使用file.lastModified(),后来才意识到这个只能获取最后修改时间,无法获取文件的创建时间。后来,参考论坛中大神(http://bbs.csdn.net/topics/220043968),使需求得以完成。
#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 ;
}
生成exe文件后就可以通过java代码来调用了。
Process proc = Runtime.getRuntime().exec("cmd /c d:\\GetFileTime1.exe d:\\t1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String data="";
for(int i=0;i<10;i++){
data=br.readLine();
System.out.println(data);
}