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

zhangbug_net 2008-04-24 04:18:23
不知道是否有什么好办法,我用dir命令获取时无法取到秒位,分钟后面就没有了.
...全文
19733 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
siyulanyu 2011-04-13
  • 打赏
  • 举报
回复
哈哈 学习了
zhangbug_net 2008-04-26
  • 打赏
  • 举报
回复
TO powerlee2008:
多谢高人的回复,我不会将c代码编译成java的loadLibrary可以调用的库,望指教.
zhangbug_net 2008-04-26
  • 打赏
  • 举报
回复
ding
我是风 2008-04-26
  • 打赏
  • 举报
回复
结果是字符串:
2008.4.25 23:16:19.890(年.月.日 时:分:秒.毫秒)
我是风 2008-04-26
  • 打赏
  • 举报
回复
精确到毫秒:
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-04-26
  • 打赏
  • 举报
回复
源文件: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;
}
我是风 2008-04-26
  • 打赏
  • 举报
回复
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
我是风 2008-04-26
  • 打赏
  • 举报
回复
在Linux中,没有文件创建时间的概念。只有文件的访问时间、修改时间、状态改变时间。也就是说不能知道文件的创建时间。
zhangbug_net 2008-04-26
  • 打赏
  • 举报
回复
导致这个的原因是MyFileTime 类的包路径跟dll中的声明没有对应
zhangbug_net 2008-04-26
  • 打赏
  • 举报
回复
OK了 多谢各位
能否在这里再讨论下linux下的方法.解决了我再开一贴感谢
sunyujia 2008-04-26
  • 打赏
  • 举报
回复
针对 UnsatisfiedLinkError

把dll放到system32下面或程序可以找到的地方
zhangbug_net 2008-04-26
  • 打赏
  • 举报
回复
我已经成功生成了dll文件,但是运行到return getFileCreationTime(fileName);时报错如下
Exception in thread "main" java.lang.UnsatisfiedLinkError: cn.imu.general.MyFileTime.getFileCreationTime(Ljava/lang/String;)Ljava/lang/String;
at cn.imu.general.MyFileTime.getFileCreationTime(Native Method)
at cn.imu.general.MyFileTime.getCreationTime(MyFileTime.java:19)
at cn.imu.general.MyFileTime.main(MyFileTime.java:23)

我应该可以确认,java程序已经调用了dll文件

我是按照powerlee2008 的调用方法
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"));
}
}

感谢sunyujia jiangnaisong :)
sunyujia 2008-04-26
  • 打赏
  • 举报
回复
有意思 楼上的,呵呵
云上飞翔 2008-04-26
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 zhangbug_net 的回复:]
TO powerlee2008:
多谢高人的回复,我不会将c代码编译成java的loadLibrary可以调用的库,望指教.
[/Quote]
答:若不会,咱们就弄个简单的。你总会把C程序编译成EXE文件吧。以下程序是在powerlee2008上简化而成的。

#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命令显示的最后修改的时间,而不是创建文件的时间。

以上仅供你参考。
sunyujia 2008-04-26
  • 打赏
  • 举报
回复
参考下这个,就知道怎么生成dll了
http://blog.csdn.net/sunyujia/archive/2007/10/19/1833621.aspx

psyl 2008-04-25
  • 打赏
  • 举报
回复
我也继续。。 我对JNI不太清楚。。 来学习
zhangbug_net 2008-04-25
  • 打赏
  • 举报
回复
TO joejoe1991:
多谢关注
zhangbug_net 2008-04-25
  • 打赏
  • 举报
回复
谁用jni实现过,或者有其他好办法,help!
joejoe1991 2008-04-25
  • 打赏
  • 举报
回复
baidu了一圈,没找到方法,想用ScriptEngine ,结果不支持ActiveXObject....
等待高人。。。
zhangbug_net 2008-04-25
  • 打赏
  • 举报
回复
多谢关注,有谁用jni实现过,介绍一下吧
加载更多回复(13)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧