JAVA按照最近访问时间输出windows文件系统目录的文件列表
因为我在JWFDV0.97版本中要在流程设计器主菜单里面给出最近访问的5个流程图的列表,所以需要在JWFD中给出查找文件的最近访问时间的功能模块
由于用JDK1.7以下的File类没有读取最近文件访问时间这个功能,因为在jdk1.6版本中,我们必须应用其它办法来实现这个功能
这几天找啊找,在 chinaunix论坛上面找到一个JAVA通过jna访问windows文件系统访问时间的类文件,感谢无名的作者,我集成了一下,给出代码,方便大家引用。。。
读取windows文件系统的访问时间,修改时间,创建时间类
import java.io.*;import java.nio.*;import java.util.Date;// Java Native Access: http://jna.dev.java.net// Test with jna-3.2.7import com.sun.jna.*;import com.sun.jna.ptr.*;import com.sun.jna.win32.*;import com.sun.jna.platform.win32.*;public class WindowsFileTime{ public static final int GENERIC_READ = 0x80000000; //public static final int GENERIC_WRITE = 0x40000000; // defined in com.sun.jna.platform.win32.WinNT public static final int GENERIC_EXECUTE = 0x20000000; public static final int GENERIC_ALL = 0x10000000; // defined in com.sun.jna.platform.win32.WinNT //public static final int CREATE_NEW = 1; //public static final int CREATE_ALWAYS = 2; //public static final int OPEN_EXISTING = 3; //public static final int OPEN_ALWAYS = 4; //public static final int TRUNCATE_EXISTING = 5; public interface MoreKernel32 extends Kernel32 { static final MoreKernel32 instance = (MoreKernel32)Native.loadLibrary ("kernel32", MoreKernel32.class, W32APIOptions.DEFAULT_OPTIONS); boolean GetFileTime (WinNT.HANDLE hFile, WinBase.FILETIME lpCreationTime, WinBase.FILETIME lpLastAccessTime, WinBase.FILETIME lpLastWriteTime); boolean SetFileTime (WinNT.HANDLE hFile, final WinBase.FILETIME lpCreationTime, final WinBase.FILETIME lpLastAccessTime, final WinBase.FILETIME lpLastWriteTime); } static MoreKernel32 win32 = MoreKernel32.instance; //static Kernel32 _win32 = (Kernel32)win32; static WinBase.FILETIME _creationTime = new WinBase.FILETIME (); static WinBase.FILETIME _lastWriteTime = new WinBase.FILETIME (); static WinBase.FILETIME _lastAccessTime = new WinBase.FILETIME (); static boolean GetFileTime (String sFileName, Date creationTime, Date lastWriteTime, Date lastAccessTime) { WinNT.HANDLE hFile = OpenFile (sFileName, GENERIC_READ); // may be WinNT.GENERIC_READ in future jna version. if (hFile == WinBase.INVALID_HANDLE_VALUE) return false; boolean rc = win32.GetFileTime (hFile, _creationTime, _lastAccessTime, _lastWriteTime); if (rc) { if (creationTime != null) creationTime.setTime (_creationTime.toLong()); if (lastAccessTime != null) lastAccessTime.setTime (_lastAccessTime.toLong()); if (lastWriteTime != null) lastWriteTime.setTime (_lastWriteTime.toLong()); } else { int iLastError = win32.GetLastError(); System.out.print ("获取文件时间失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError)); } win32.CloseHandle (hFile); return rc; } static boolean SetFileTime (String sFileName, final Date creationTime, final Date lastWriteTime, final Date lastAccessTime) { WinNT.HANDLE hFile = OpenFile (sFileName, WinNT.GENERIC_WRITE); if (hFile == WinBase.INVALID_HANDLE_VALUE) return false; ConvertDateToFILETIME (creationTime, _creationTime); ConvertDateToFILETIME (lastWriteTime, _lastWriteTime); ConvertDateToFILETIME (lastAccessTime, _lastAccessTime); //System.out.println ("creationTime: " + creationTime); //System.out.println ("lastWriteTime: " + lastWriteTime); //System.out.println ("lastAccessTime: " + lastAccessTime); //System.out.println ("_creationTime: " + _creationTime); //System.out.println ("_lastWriteTime: " + _lastWriteTime); //System.out.println ("_lastAccessTime: " + _lastAccessTime); boolean rc = win32.SetFileTime (hFile, creationTime==null?null:_creationTime, lastAccessTime==null?null:_lastAccessTime, lastWriteTime==null?null:_lastWriteTime); if (! rc) { int iLastError = win32.GetLastError(); System.out.print ("设置文件时间失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError)); } win32.CloseHandle (hFile); return rc; } static void ConvertDateToFILETIME (Date date, WinBase.FILETIME ft) { if (ft != null) { long iFileTime = 0; if (date != null) { iFileTime = WinBase.FILETIME.dateToFileTime (date); ft.dwHighDateTime = (int)((iFileTime >> 32) & 0xFFFFFFFFL); ft.dwLowDateTime = (int)(iFileTime & 0xFFFFFFFFL); } else { ft.dwHighDateTime = 0; ft.dwLowDateTime = 0; } } } static WinNT.HANDLE OpenFile (String sFileName, int dwDesiredAccess) { WinNT.HANDLE hFile = win32.CreateFile ( sFileName, dwDesiredAccess, 0, null, WinNT.OPEN_EXISTING, 0, null ); if (hFile == WinBase.INVALID_HANDLE_VALUE) { int iLastError = win32.GetLastError(); System.out.print (" 打开文件失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError)); } return hFile; } static String GetWindowsSystemErrorMessage (int iError) { char[] buf = new char[255]; CharBuffer bb = CharBuffer.wrap (buf); //bb.clear (); //PointerByReference pMsgBuf = new PointerByReference (); int iChar = win32.FormatMessage ( WinBase.FORMAT_MESSAGE_FROM_SYSTEM //| WinBase.FORMAT_MESSAGE_IGNORE_INSERTS //|WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER , null, iError, 0x0804, bb, buf.length, //pMsgBuf, 0, null ); //for (int i=0; i<iChar; i++) //{ // System.out.print (" "); // System.out.print (String.format("%02X", buf[i]&0xFFFF)); //} bb.limit (iChar); //System.out.print (bb); //System.out.print (pMsgBuf.getValue().getString(0)); //win32.LocalFree (pMsgBuf.getValue()); return bb.toString (); } //测试读取一个文件的最后访问时间 public static void main (String[] args) throws Exception { /*boolean rc; java.sql.Timestamp at = new java.sql.Timestamp(0); String filename = "E:\\java\\jwfd\\jwfdv0.96\\jwfd v0.96\\gxl_temp\\test006.gxl"; System.out.println(at); */ } //add by comsci 2013.2.28 public String GetFilelastAccessTime(String filename){ String act = ""; java.sql.Timestamp at = new java.sql.Timestamp(0); boolean rc; rc = GetFileTime (filename, null, null, at); if (rc){ act = at.toString(); }else{ act = "读取文件失败"; } return act; } /* if (args.length == 0) { System.out.println ("获取 Windows 的文件时间(创建时间、最后修改时间、最后访问时间)"); System.out.println ("用法:"); System.out.println (" java -cp .;..;jna.jar;platform.jar WindowsFileTime [文件名1] [文件名2]..."); return; } boolean rc; java.sql.Timestamp ct = new java.sql.Timestamp(0); java.sql.Timestamp wt = new java.sql.Timestamp(0); java.sql.Timestamp at = new java.sql.Timestamp(0); for (String sFileName : args) { System.out.println ("文件 " + sFileName); rc = GetFileTime (sFileName, ct, wt, at); if (rc) { System.out.println (" 创建时间:" + ct); System.out.println (" 修改时间:" + wt); System.out.println (" 访问时间:" + at); } else { //System.out.println ("GetFileTime 失败"); } //wt.setTime (System.currentTimeMillis()); wt = java.sql.Timestamp.valueOf("2010-07-23 00:00:00"); rc = SetFileTime (sFileName, null, wt, null); if (rc) { System.out.println ("SetFileTime (最后修改时间) 成功"); } else { //System.out.println ("SetFileTime 失败"); } } }*/}
将某个目录下面最近访问的文件写入map
public static Map<Integer, String> ListFileOpenRecent(String filepath,Map<Integer, String> fileMap) throws Exception {Map<Integer, String> filelist1 = new HashMap<Integer, String>();if (fileMap == null) {fileMap = new HashMap<Integer, String>();}try {Map<Integer, String> filemap = readfile(filepath, null);for (int i = 0; i < filemap.size(); i++) {filelist1.put(new Long((ftc.cov_datetomilltime(ftc.get_systime()) - ftc.cov_datetomilltime(wft.GetFilelastAccessTime(filemap.get(i))))).intValue(),filemap.get(i));}} catch (Exception ex) {}// Collections.sort(filelist1);// System.out.println(filelist1.toString());return filelist1;}
这个方法要和上面这个方法放在一起
public static Map<Integer, String> readfile(String filepath, Map<Integer, String> pathMap) throws Exception { if (pathMap == null) { pathMap = new HashMap<Integer, String>(); } File file = new File(filepath); // 文件 if (!file.isDirectory()) { pathMap.put(pathMap.size(), file.getPath()); } else if (file.isDirectory()) { // 如果是目录, 遍历所有子目录取出所有文件名 String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "/" + filelist[i]); if (!readfile.isDirectory()) { pathMap.put(pathMap.size(), readfile.getPath()); } else if (readfile.isDirectory()) { // 子目录的目录 readfile(filepath + "/" + filelist[i], pathMap); } } } return pathMap; }
主函数调用,把下面的文件目录修改为自己的文件目录
public static void main(String argv[]) { apptools apt = new apptools();try { //应用TREEMAP按照KEY值排序,输出按照最近访问时间(最少秒数)排序的文件列表TreeMap treemap = new TreeMap(apt.ListFileOpenRecent("E:\\java\\jwfd\\jwfdv0.96\\jwfd v0.96\\gxl_temp", null)); Iterator it= treemap.entrySet().iterator();while(it.hasNext()){ System.out.println(it.next());}} catch (Exception e) {// TODO: handle exception}
//时间格式转换为秒函数 为适用于map读取Key,把long值转换为int值,原来的毫秒变为秒
上面的方法要用到的代码
public long cov_datetomilltime(String time){ long ti = 0; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date2 = null; date2 = dateFormat.parse(time); ti = date2.getTime()/1000; } catch (ParseException ex) { } return ti; } public String get_systime() { String time = ""; java.util.Date date = new java.util.Date(); time = wudt.getCurrentDate(date); return time; }
附件是需要的jar包。。。。感谢CSDN的朋友 如果代码有什么问题,请给我发消息,我把整个代码包给大家。。。