获得文件的创建时间(精确到时分秒)

  最近的项目中遇到一个需求,要求判断文件是否被修改。这下犯了难。一开始打算使用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);
	         }



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值