题
我想提取 jpg 文件的创建日期。Java 有 File 对象的 lastModified 方法,但似乎不支持从文件中提取创建日期。我相信这些信息存储在文件中,因为当我将鼠标指针悬停在 Win XP 中的文件上时看到的日期与我在 DOS 中使用 JNI 和“dir /TC”在文件上获得的日期不同。
其他提示
我使用这个元数据库: http://www.drewnoakes.com/code/exif/
似乎工作得很好,但请记住,并非所有 JPEG 图像都有此信息,因此它不可能 100% 万无一失。
如果 EXIF 元数据不包含创建日期,那么您可能不得不使用 Java 的 lastUpdated - 除非您想求助于 Runtime.exec(...) 并使用系统函数来查找(我不会)不过,不推荐这个!)
下面的代码示例询问用户文件路径,然后输出创建日期和时间:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(final 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 /a "+fname);
// 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,15));
}
catch(StringIndexOutOfBoundsException se)
{
System.out.println("File not found");
}
}
catch (IOException ioe){ System.err.println(ioe); }
catch (Throwable t) { t.printStackTrace();}
}
}
不隶属于 StackOverflow