日期:2014-05-17 浏览次数:20969 次
import java.io.*; import java.nio.*; import java.util.Date; // Java Native Access: http://jna.dev.java.net // Test with jna-3.2.7 import 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.o