



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
/前言:在我寫這篇文章之前,我說說我的碰到這個問題的起因。公司讓我寫一個jar包,需要獲得指定文件的創建日期(日期必須是完整地年月日時分秒)。對于javajdk1.7來說很容易,因為1.7給了我們獲得創建日期的接口。但是對于jdk1.6來說,那就是一個老大難,沒有提供,只提供了獲得最后修改日期的接口。個人收集整理勿做商業用途對于jdk1.6,當然我也查過,請教過好多人,給出了解決辦法,那就是使用Java的Runtime去調用當前操作系統的指令獲得文件的創建日期:個人收集整理勿做商業用途例如在windows下,publicStringgetFileCreateDate(File_file){Filefile=_file;
try{
Processls_proc=Runtime.getRuntime().exec(
"cmd.exe/cdir"+file.getAbsolutePath()+"/tc");
個人收集整理勿做商業用途BufferedReaderbr=newBufferedReader(newInputStreamReader(ls_proc.getInputStream()));
個人收集整理勿做商業用途for(inti=0;i<5;i++){
br.readLine();
}
Stringstuff=br.readLine();
StringTokenizerst=newStringTokenizer(stuff);
StringdateC=st.nextToken();
Stringtime=st.nextToken();
Stringdatetime=dateC.concat(time);
SimpleDateFormatformatter1=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
個人收集整理勿做商業用途SimpleDateFormatformatter2=newSimpleDateFormat(
個人收集整理勿做商業用途"yyyy/MM/ddHH:mm");
datetime=formatter1.format(formatter2.parse(datetime));
個人收集整理勿做商業用途
br.close();
returndatetime;
}catch(Exceptione){
returnnull;
}
}引用:/question/535215288.html但是這個方法只能或得到文件的年月日時分,不能獲得到秒。為了獲得到秒,最后使用了jni。步驟:1、使用c和C++寫一個獲得文件創建時間的類#pragmaonce//Modifythefollowingdefinesifyouhavetotargetaplatformpriortotheonesspecifiedbelow.//RefertoMSDNforthelatestinfooncorrespondingvaluesfordifferentplatforms.#ifndefWINVER//AllowuseoffeaturesspecifictoWindowsXPorlater.#defineWINVER0x0501//ChangethistotheappropriatevaluetotargetotherversionsofWindows.#endif#ifndef_WIN32_WINNT//AllowuseoffeaturesspecifictoWindowsXPorlater.#define_WIN32_WINNT0x0501//ChangethistotheappropriatevaluetotargetotherversionsofWindows.#endif#ifndef_WIN32_WINDOWS//AllowuseoffeaturesspecifictoWindows98orlater.#define_WIN32_WINDOWS0x0410//ChangethistotheappropriatevaluetotargetWindowsMeorlater.#endif#ifndef_WIN32_IE//AllowuseoffeaturesspecifictoIE6.0orlater.#define_WIN32_IE0x0600//ChangethistotheappropriatevaluetotargetotherversionsofIE.#endif#defineWIN32_LEAN_AND_MEAN//Excluderarely-usedstufffromWindowsheaders//WindowsHeaderFiles:#include<windows.h>個人收集整理勿做商業用途
//WinFileTime.cpp:DefinestheentrypointfortheDLLapplication.//#include"stdafx.h"#include"FileClass/FileTimeEx.h"#ifdef_MANAGED#pragmamanaged(push,off)#endifBOOLAPIENTRYDllMain(HMODULEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){returnTRUE;}JNIEXPORTjstringJNICALLJava_checkfile_WinFileTime_getFileCreationTime(JNIEnv*env,jobjectcls,jstringFileName){HANDLEhFile;FILETIMEcreationTime;FILETIMElastAccessTime;FILETIMElastWriteTime;FILETIMEcreationLocalTime;SYSTEMTIMEcreationSystemTime;jstringresult;charfileTimeString[30];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)returnenv->NewStringUTF("");if(GetFileTime(hFile,&creationTime,&lastAccessTime,&lastWriteTime)){if(FileTimeToLocalFileTime(&creationTime,&creationLocalTime)){if(FileTimeToSystemTime(&creationLocalTime,&creationSystemTime)){sprintf_s(fileTimeString,"d-d-dd:d:d\0",creationSystemTime.wYear,creationSystemTime.wMonth,creationSystemTime.wDay,creationSystemTime.wHour,creationSystemTime.wMinute,creationSystemTime.wSecond),result=env->NewStringUTF(fileTimeString);}elseresult=env->NewStringUTF("");}elseresult=env->NewStringUTF("");}elseresult=env->NewStringUTF("");CloseHandle(hFile);returnresult;}#ifdef_MANAGED#pragmamanaged(pop)#endif個人收集整理勿做商業用途2、然后生成動態鏈接文件(*.dll),將這個文件放到C:\Windows\System32和你的javajdk目錄下D:\ProgramFiles\Java\jdk1.6.0_18\jre\bin。個人收集整理勿做商業用途3、java文件中寫法publicfinalclassWinFileTime{static{System.loadLibrary("WinFileTime");}privatestaticnativeStringgetFileCreationTime(StringfileName);publicstaticStringgetCreationTime(StringfileName){returngetFileCreationTime(fileName);}}}個人收集整理勿做商業用途注意:頭文件與類文件的方法名必須與java中的類名一直,如:c中是Java_checkfile_WinFileTime_getFileCreationTime,那么java中就是checkfile.WinFileTime.getFileCreationTime(checkfile是包名,WinFileTime是類名,getFileCreationTime是方法名).個人收集整理勿做商業用途jdk1.7方法:下載一個jdk1.7幫助文檔,看看這個類BasicFileAttributeView。Pathpath=Paths.get(filePath);
BasicFileAttributeViewbasicview=Files.getFi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 回爐培訓實施方案
- 北京市大學線性代數(行列式與矩陣)期末考試真題解析
- 2025年校園體育器材借用與歸還的標準化執行標準
- 2025年小學數學應用題競賽試卷:一年級上學期應用題解題競賽
- 2025年教師進修項目審批流程及規范
- 護理病例討論實施要點
- 2025年資產評估實務(二)無形資產評估模擬試卷(含評估創新)
- 勞動課標解讀課件
- 培訓講座課件
- 針對性訓練計算機二級Python試題及答案
- 非遺扎染創新創業計劃書
- 超星爾雅學習通《先秦諸子導讀(浙江大學)》2025章節測試附答案
- 江蘇社工考試試題及答案
- 2025年勞務合同模板電子版簡短一點
- 二級建造師繼續教育題庫(帶答案)
- 市場監管投訴舉報培訓
- 《新能源乘用車二手車鑒定評估技術規范 第1部分:純電動》
- 課題申報參考:西藏地方與祖國關系史融入當地高校“中華民族共同體概論”課教學研究
- 【MOOC】《C++程序設計基礎》(華中科技大學)章節作業中國大學慕課答案
- 《南方航空公司匯率風險管理策略案例分析》
- 病房心臟驟停應急演練
評論
0/150
提交評論