Java和jni(获得指定文件创建时间)

上传人:hs****ma 文档编号:510951937 上传时间:2023-08-30 格式:DOCX 页数:4 大小:14.22KB
返回 下载 相关 举报
Java和jni(获得指定文件创建时间)_第1页
第1页 / 共4页
Java和jni(获得指定文件创建时间)_第2页
第2页 / 共4页
Java和jni(获得指定文件创建时间)_第3页
第3页 / 共4页
Java和jni(获得指定文件创建时间)_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Java和jni(获得指定文件创建时间)》由会员分享,可在线阅读,更多相关《Java和jni(获得指定文件创建时间)(4页珍藏版)》请在金锄头文库上搜索。

1、文档供参考,可复制、编制,期待您的好评与关注! 前言:在我写这篇文章之前,我说说我的碰到这个问题的起因。公司让我写一个jar包,需要获得指定文件的创建日期(日期必须是完整地年月日时分秒)。对于java jdk1.7来说很容易,因为1.7给了我们获得创建日期的接口。但是对于jdk1.6来说,那就是一个老大难,没有提供,只提供了获得最后修改日期的接口。对于jdk1.6,当然我也查过,请教过好多人,给出了解决办法,那就是使用Java的Runtime去调用当前操作系统的指令获得文件的创建日期:例如在windows下,public String getFileCreateDate(File _file)

2、 File file = _file; try Process ls_proc = Runtime.getRuntime().exec( cmd.exe /c dir + file.getAbsolutePath() + /tc); BufferedReader br = new BufferedReader(new InputStreamReader( ls_proc.getInputStream(); for (int i = 0; i 5; i+) br.readLine(); String stuff = br.readLine(); StringTokenizer st = new

3、StringTokenizer(stuff); String dateC = st.nextToken(); String time = st.nextToken(); String datetime = dateC.concat(time); SimpleDateFormat formatter1 = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss); SimpleDateFormat formatter2 = new SimpleDateFormat( yyyy/MM/ddHH:mm); datetime = formatter1.format(form

4、atter2.parse(datetime); br.close(); return datetime; catch (Exception e) return null; 引用:http:/ 1、使用c和C+ 写一个获得文件创建时间的类#pragma once / Modify the following defines if you have to target a platform prior to the ones specified below. / Refer to MSDN for the latest info on corresponding values for differ

5、ent platforms. #ifndef WINVER / Allow use of features specific to Windows XP or later. #define WINVER 0x0501 / Change this to the appropriate value to target other versions of Windows. #endif #ifndef _WIN32_WINNT / Allow use of features specific to Windows XP or later. #define _WIN32_WINNT 0x0501 /

6、Change this to the appropriate value to target other versions of Windows. #endif #ifndef _WIN32_WINDOWS / Allow use of features specific to Windows 98 or later. #define _WIN32_WINDOWS 0x0410 / Change this to the appropriate value to target Windows Me or later. #endif #ifndef _WIN32_IE / Allow use of

7、 features specific to IE 6.0 or later. #define _WIN32_IE 0x0600 / Change this to the appropriate value to target other versions of IE. #endif #define WIN32_LEAN_AND_MEAN / Exclude rarely-used stuff from Windows headers / Windows Header Files: #include / WinFileTime.cpp : Defines the entry point for

8、the DLL application. / #include stdafx.h #include FileClass/FileTimeEx.h #ifdef _MANAGED #pragma managed(push, off) #endif BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) return TRUE; JNIEXPORT jstring JNICALL Java_checkfile_WinFileTime_getFileCreationTime(JNIEn

9、v *env, jobject cls, jstring FileName) HANDLE hFile; FILETIME creationTime; FILETIME lastAccessTime; FILETIME lastWriteTime; FILETIME creationLocalTime; SYSTEMTIME creationSystemTime; jstring result; char fileTimeString30; hFile = CreateFileA(char *)env-GetStringUTFChars(FileName, 0), GENERIC_READ,

10、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(&creationLocalTim

11、e, &creationSystemTime) sprintf_s(fileTimeString, d-d-d d:d:d0, creationSystemTime.wYear, creationSystemTime.wMonth, creationSystemTime.wDay, creationSystemTime.wHour, creationSystemTime.wMinute, creationSystemTime.wSecond), result = env-NewStringUTF(fileTimeString); else result = env-NewStringUTF()

12、; else result = env-NewStringUTF(); else result = env-NewStringUTF(); CloseHandle(hFile); return result; #ifdef _MANAGED #pragma managed(pop) #endif2、然后生成动态链接文件(*.dll),将这个文件放到C:WindowsSystem32和你的java jdk目录下D:Program FilesJavajdk1.6.0_18jrebin。 3、java文件中写法 public final class WinFileTime static System

13、.loadLibrary(WinFileTime); private static native String getFileCreationTime(String fileName); public static String getCreationTime(String fileName) return getFileCreationTime(fileName); 注意:头文件与类文件的方法名必须与java中的类名一直,如:c中是Java_checkfile_WinFileTime_getFileCreationTime,那么java中就是checkfile.WinFileTime.get

14、FileCreationTime(checkfile是包名,WinFileTime是类名,getFileCreationTime是方法名).jdk1.7方法:下载一个jdk1.7帮助文档,看看这个类BasicFileAttributeView 。Path path=Paths.get(filePath); BasicFileAttributeView basicview=Files.getFileAttributeView(path, BasicFileAttributeView.class,LinkOption.NOFOLLOW_LINKS );BasicFileAttributes attr = basicview.readAttributes();Date createDate = new Date(attr.creationTime().toMillis();/创建时间 /

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 国内外标准规范

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号