2008-08-28 9 views
23

Gibt es eine einfache Möglichkeit, die Erstellungszeit einer Datei mit Java zu ermitteln? Die Dateiklasse hat nur eine Methode, um die "letzte Änderung" zu erhalten. Laut einigen Ressourcen, die ich bei Google gefunden habe, bietet die File-Klasse keine getCreationTime() -Methode, weil nicht alle Dateisysteme die Idee einer Erstellungszeit unterstützen.Wie finde ich die Erstellungszeit einer Datei mit Java?

Die einzige funktionierende Lösung, die ich gefunden involes die der Zeilenbefehl Beschuss aus und den „dir“ Befehl ausgeführt wird, die aussieht wie es die Erstellungszeit der Datei ausgibt. Ich denke, das funktioniert, ich muss nur Windows unterstützen, aber es scheint sehr fehleranfällig für mich.

Gibt es Bibliotheken von Drittanbietern, die die benötigten Informationen bereitstellen?

Update: Am Ende, ich glaube nicht, dass es wert ist es für mich die dritte Party-Bibliothek zu kaufen, aber ihre API scheint ziemlich gut, so ist es wahrscheinlich eine gute Wahl für alle andere, dass dieses Problem hat.

+0

ich die [Apache Commons VFS] geprüft (http://commons.apache.org/vfs/), in dem eine virtuelle Datei System kann eine reichhaltige Menge von Fähigkeiten exportieren. Leider ist jedoch keine CREATION_TIME-Fähigkeit definiert. –

+0

AFAIK, NIO-2 löst dieses Problem, ist aber noch nicht veröffentlicht (sollte in der 7. Version sein). Siehe zum Beispiel http://java.sun.com/developer/technicalArticles/javase/nio/ und http://today.java.net/article/2009/10/14/sweeping-file-system-nio-2 –

Antwort

1

Ich mag die Antwort auf jGuru, die die Option der Verwendung JNI auflistet, um die Antwort zu erhalten. Dies könnte sich als schneller herausstellen und Sie könnten auf andere Situationen wie diese treffen, die speziell für Windows implementiert werden müssen.

Auch, wenn Sie jemals in den Hafen einer anderen Plattform benötigen, können Sie den Port Ihrer Bibliothek als auch und gerade haben sie -1 zurück für die Antwort auf diese Frage zu * ix.

2

Ich habe das selbst untersucht, aber ich brauche etwas, das auf Windows/* nix-Plattformen funktioniert.

Ein SO Post enthält einige Links zu Posix JNI implementations.

Insbesondere JNA-POSIX implementiert methods for getting file stats mit Implementierungen für Windows, BSD, Solaris, Linux und OSX.

Alles in allem sieht es sehr vielversprechend aus, also werde ich es bald auf meinem eigenen Projekt ausprobieren.

7

Ich habe vor ein paar Tagen eine kleine Test-Klasse schrieb, wünschen es Ihnen helfen können:

// Get/Set windows file CreationTime/LastWriteTime/LastAccessTime 
// Test with jna-3.2.7 
// [http://maclife.net/wiki/index.php?title=Java_get_and_set_windows_system_file_creation_time_via_JNA_(Java_Native_Access)][1] 

import java.io.*; 
import java.nio.*; 
import java.util.Date; 

// Java Native Access library: jna.dev.java.net 
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.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 
    { 
     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 失败"); 
      } 
     } 
    } 
} 
+0

Hat mir so sehr geholfen! Vielen Dank! –

2
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 


public class CreateDateInJava { 
    public static void main(String args[]) { 
     try { 

      // get runtime environment and execute child process 
      Runtime systemShell = Runtime.getRuntime(); 
      BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println("Enter filename: "); 
      String fname = (String) br1.readLine(); 
      Process output = systemShell.exec("cmd /c dir \"" + fname + "\" /tc"); 

      System.out.println(output); 
      // open reader to get output from process 
      BufferedReader br = new BufferedReader(new InputStreamReader(output.getInputStream())); 

      String out = ""; 
      String line = null; 

      int step = 1; 
      while ((line = br.readLine()) != null) { 
       if (step == 6) { 
        out = line; 
       } 
       step++; 
      } 

      // display process output 
      try { 
       out = out.replaceAll(" ", ""); 
       System.out.println("CreationDate: " + out.substring(0, 10)); 
       System.out.println("CreationTime: " + out.substring(10, 16) + "m"); 
      } catch (StringIndexOutOfBoundsException se) { 
       System.out.println("File not found"); 
      } 
     } catch (IOException ioe) { 
      System.err.println(ioe); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
     } 
    } 
} 

/** 
D:\Foldername\Filename.Extension 

Ex: 
Enter Filename : 
D:\Kamal\Test.txt 
CreationDate: 02/14/2011 
CreationTime: 12:59Pm 

*/ 
2

Die javaxt-core Bibliothek eine File-Klasse enthält, die verwendet werden können, Dateiattribute abzurufen, einschließlich der Erschaffungszeit. Beispiel:

javaxt.io.File file = new javaxt.io.File("/temp/file.txt"); 
System.out.println("Created: " + file.getCreationTime()); 
System.out.println("Accessed: " + file.getLastAccessTime()); 
System.out.println("Modified: " + file.getLastModifiedTime()); 

Funktioniert mit Java 1.5 und höher.

21

Mit der Veröffentlichung von Java 7 gibt es eine eingebaute Möglichkeit, dies zu tun:

Path path = Paths.get("path/to/file"); 
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class); 
FileTime creationTime = attributes.creationTime(); 

Es ist wichtig zu beachten, dass nicht alle Betriebssysteme diese Informationen zur Verfügung stellen. Ich glaube in diesen Fällen gibt dies die mtime zurück, die die letzte modifizierte Zeit ist.

Windows bietet Erstellungszeit.

0

Dies ist ein einfaches Beispiel in Java, BasicFileAttributes-Klasse:

Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt"); 
    BasicFileAttributes attr; 
    try { 
     attr = Files.readAttributes(path, BasicFileAttributes.class); 
     System.out.println("File creation time: " + attr.creationTime()); 
    } catch (IOException e) { 
     System.out.println("oops un error! " + e.getMessage()); 
    }