java如何获取文件的创建日期
package com.mkyong.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=0i<6i++){
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
java没有函数能直接拿到文件的创建时间lastModified()只能获得最后一次修改时间想要拿到创建时间必须依赖Windows系统的API,通过调CMD命令实现,
代码如下:
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec(
"cmd /C dir d:\\test.txt /tc")
InputStream is = p.getInputStream()
BufferedReader br = new BufferedReader(new InputStreamReader(is))
String str
int i = 0
while ((str = br.readLine()) != null) {
i++
if (i == 6) {
System.out.println(str.substring(0, 17))
}
}
} catch (java.io.IOException exc) {
exc.printStackTrace()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)