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

以上仅供你参考。

Java获得文件的创建时间(精确到秒)的更多相关文章

  1. 目录和文件 按创建时间排序du -h --time --max-depth=1 . |sort -r -t $'\t' -k 2 Linux查看文件夹大小,并按文件夹创建时间排序

    目录和文件 按创建时间排序 # du -h --time --max-depth=1 . |sort -r -t $'\t' -k 230M 2020-04-01 14:54 .28K 2020-04 ...

  2. Linux如何查找文件的创建时间

    Linux的文件能否找到文件的创建时间取决于文件系统类型,在ext4之前的早期文件系统中(ext.ext2.ext3),文件的元数据不会记录文件的创建时间,它只会记录访问时间.修改时间.更改时间(状态 ...

  3. Python 获取文件的创建时间,修改时间和访问时间

    # 用到的知识# os.path.getatime(file) 输出文件访问时间# os.path.getctime(file) 输出文件的创建时间# os.path.getmtime(file) 输 ...

  4. C#/.NET 读取或修改文件的创建时间和修改时间

    手工在博客中添加 Front Matter 文件头可是个相当费事儿的做法,这种事情就应该自动完成. .NET 中提供了非常方便的修改文件创建时间的方法,使用这种方法,能够帮助自动完成一部分文件头的编写 ...

  5. java操作文件的创建、删除、遍历

    java操作文件的创建.删除.遍历: package test; import java.io.File; import java.io.IOException; import java.util.A ...

  6. python文件夹遍历,文件操作,获取文件修改创建时间

    在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前 ...

  7. Linux 如何用命令查看binlog文件的创建时间

    目录 背景 分析 方法 注意 背景 MySQL在26日 16:23:49产生了大量的慢查询,在这段时间内,binlog文件刷新的很快(查看慢日志是mysql DML并发比较多),想知道写完一个binl ...

  8. python 修改文件的创建时间、修改时间、访问时间

    目录 python 修改文件创建.修改.访问时间 方案一 方案二(无法修改文件创建时间) python 修改文件创建.修改.访问时间 突如其来想知道一下 python 如何修改文件的属性(创建.修改. ...

  9. TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间

    使用函数: System.IOUtils.TDirectory.GetCreationTime//获取创建时间 System.IOUtils.TDirectory.SetCreationTime//设 ...

随机推荐

  1. java枚举类

    enum关键字用于定义枚举类,若枚举只有一个成员, 则可以作为一种单例模式的实现方式.   枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰. 枚举类的使用 priva ...

  2. 纯手写分页控件CSS+JS+SQL

    Asp.net中虽然用DataPager配合ListView可以实现分页显示,但是有时候由于开发环境等问题不能用到DataPager控件,那么自己手工写一个分页控件就很有必要了,当然,最重要的是通用性 ...

  3. Cannot change network to bridged: There are no un-bridged host network adapters解决方法

    首先,在你安装上了虚拟机后要确保你也安装了桥接的协议,这可以通过点击右键“网上邻居”,在其中可以看到有两个虚拟出来的网络一个VMnet1,另一个是VMnet8, 如下图所示. 如果没有安装,可以通过下 ...

  4. Http Framework

    http request clientVolley https://android.googlesource.com/platform/frameworks/volley聚划算用的litehttp h ...

  5. C/C++ memmove 和 memcpy

    这两个函数用于拷贝字符串或者一段连续的内存,函数原型: void * memcpy ( void * destination, const void * source, size_t num ); v ...

  6. FineUI疑难杂症

    1. 导出excel操作:因为fineui里的按钮自带异步功能,所以要把 EnableAjax="false" DisableControlBeforePostBack=" ...

  7. UIkit框架之UItextfield

    1.继承链:UIcontrol:UIview:UIresponder:NSObject 2.成为第一响应者:[text becomeFirstResponder];  //让该文本成为第一响应者 3. ...

  8. php大力力 [027节] 被百度收录较好的几个视频网站示例

    php大力力 [027节] 被百度收录较好的几个视频网站示例 56网 很清晰 :2014 兄弟连高洛峰 PHP教程14.1.7 在PHP脚本中操作MySQL数据库4_视频在线观看 - 56.com 土 ...

  9. Qemu+gdb跟踪内核源码

    1.编译安装Qemu Qemu源码下载地址:http://wiki.qemu.org/Download linux下可以直接用wget下载: wget http://wiki.qemu.org/dow ...

  10. js 检测 flash插件以及版本号 通用所有浏览器

    var fls = flashChecker(); if (fls.h) { if (fls.v < parseFloat('8.0')) { alert("您当前的flash pla ...