Javaでファイル作成日を取得する方法
Javaでファイル作成日を取得する公式の方法はありません。 ただし、次の回避策を使用して、Windowsプラットフォームでファイルの作成日を取得できます。
それがどのように働きますか
Windowsコマンドプロンプトで、ファイル作成日を一覧表示するコマンドを入力します。
C:\>cmd /c dir c:\logfile.log /tc Volume in drive C has no label. Volume Serial Number is 0410-1EC3 Directory of c:\ 31/05/2010 08:05 14 logfile.log 1 File(s) 14 bytes 0 Dir(s) 35,389,460,480 bytes free
「31/05/2010 08:05」はあなたが必要とするものです。 アイデアは、Javaの「Runtime.getRuntime().exec」を使用して上記のコマンドを実行し、出力を保持して、日付と時刻を取得するまで行ごとに解析することです。
例
この例では、ファイルの作成日(c:\ logfile.log)を取得します。
package com.example.file; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class GetFileCreationDateExample { public static void main(String[] args) { try{ Process proc = Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc"); BufferedReader br = new BufferedReader( new InputStreamReader(proc.getInputStream())); String data =""; //it's quite stupid but work for(int i=0; i<6; i++){ data = br.readLine(); } System.out.println("Extracted value : " + data); //split by space StringTokenizer st = new StringTokenizer(data); String date = st.nextToken();//Get date String time = st.nextToken();//Get time System.out.println("Creation Date : " + date); System.out.println("Creation Time : " + time); }catch(IOException e){ e.printStackTrace(); } } }
結果
Extracted value : 31/05/2010 08:05 14 logfile.log Creation Date : 31/05/2010 Creation Time : 08:05