2个回答
推荐于2016-05-11 · 知道合伙人数码行家
关注
展开全部
在java中没有正式的方法来获取文件的创建时间,但是,可以使用下面的解决方法在Windows平台上获取文件的创建日期。
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=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
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=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
展开全部
//取不到创建时间,只能取到文件最后一次被修改的时间。
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
public class Test {
public static void main(String[] args) throws IOException {
File file =new File("Test.java");
Long time =file.lastModified();
Calendar cd = Calendar.getInstance();
cd.setTimeInMillis(time);
System.out.println(cd.getTime());
}
}
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
public class Test {
public static void main(String[] args) throws IOException {
File file =new File("Test.java");
Long time =file.lastModified();
Calendar cd = Calendar.getInstance();
cd.setTimeInMillis(time);
System.out.println(cd.getTime());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |