



付費下載
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
/前言:在我寫這篇文章之前,我說說我的碰到這個問題的起因。公司讓我寫一個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屆青海省西寧市沛西中學物理高二第二學期期末統考模擬試題含解析
- 信息技術驅動數學德育滲透計劃
- 科級干部綜合素質培訓心得體會
- 千枚巖棄渣側限壓縮特性與顆粒重構三維結構模型研究
- 天門山污水處理廠二期擴建項目環境影響報告書
- 婦產科學 妊娠合并心臟病
- -衛生資格-副高-疾病控制-副高-章節練習-慢性非傳染性疾病控制-試題(單選題)(共1125題)
- 骨質疏松病人的護理
- 高中英語全國高考考綱詞匯3600匯總
- GB/T 35068-2018油氣管道運行規范
- GB/T 13277.7-2021壓縮空氣第7部分:活性微生物含量測量方法
- 2023年婁底冷水江市廣播電視臺(融媒體中心)招聘筆試模擬試題及答案解析
- 特勞特戰略定位總裁課程課件
- 陳寶光-TTT課程開發與設計(講義)V2.1
- 六年級開學主題班會課件
評論
0/150
提交評論