资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
文档供参考,可复制、编制,期待您的好评与关注! 前言:在我写这篇文章之前,我说说我的碰到这个问题的起因。公司让我写一个jar包,需要获得指定文件的创建日期(日期必须是完整地年月日时分秒)。对于java jdk1.7来说很容易,因为1.7给了我们获得创建日期的接口。但是对于jdk1.6来说,那就是一个老大难,没有提供,只提供了获得最后修改日期的接口。对于jdk1.6,当然我也查过,请教过好多人,给出了解决办法,那就是使用Java的Runtime去调用当前操作系统的指令获得文件的创建日期:例如在windows下,public String getFileCreateDate(File _file) 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 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(formatter2.parse(datetime); br.close(); return datetime; catch (Exception e) return null; 引用:http:/zhidao.baidu.com/question/535215288.html但是这个方法只能或得到文件的年月日时分,不能获得到秒。为了获得到秒,最后使用了jni。步骤: 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 different 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 / 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 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 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(JNIEnv *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, 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_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(); 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.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.getFileCreationTime(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();/创建时间 /
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号