當前位置:首頁 » 編程語言 » java修改文件時間

java修改文件時間

發布時間: 2025-07-10 19:50:30

java中如何得到文件的創建時間&最後修改時間

java中只能用java.io.File獲得文件的最後修改時間,如下:

比如我在D盤有個文件夾a,現在要獲取其創建時間:
File file = new File("D:\a");
long time = file.lastModified();//返迴文件最後修改時間,是以個long型毫秒數
String ctime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(time));
System.out.println(ctime);


想要獲得文件的創建時間,那麼只能用java去掉命令實現了,若為windows系統,想要拿到創建時間必須依賴Windows系統的API,通過調CMD命令實現,代碼如下:

publicstaticvoidmain(String[]args){
try{
Processp=Runtime.getRuntime().exec("cmd/Cdird:\test.txt/tc");
InputStreamis=p.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringstr;
inti=0;
while((str=br.readLine())!=null){
i++;
if(i==6){
System.out.println(str.substring(0,17));
}
}
}catch(java.io.IOExceptionexc){
exc.printStackTrace();
}
}

⑵ java 如何獲得一個文件夾的創建時間 具體點

System.out.println("文件/文件夾的最後修改時間:" + new Date(file.lastModified()).toLocaleString());

⑶ JAVA怎樣實現獲取文件創建時間啊,求幫助。

Java只能讀取到文件的最後修改時間,不能獲取創建時間,

創建時間是利用了cmd命令獲取的:

publicclassFileTest{

publicstaticvoidmain(String[]args){
getCreateTime("d:\test-1.txt");
getModifiedTime("d:\test-1.txt");
}

publicstaticvoidgetCreateTime(StringfilePath){
StringstrTime=null;
try{
Processp=Runtime.getRuntime().exec("cmd/Cdir"+filePath+"/tc");
InputStreamis=p.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringline;
while((line=br.readLine())!=null){
if(line.endsWith(".txt")){
strTime=line.substring(0,17);
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}
System.out.println("創建時間"+strTime);
}
(StringfilePath){
longtime=newFile(filePath).lastModified();
Stringctime=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss").format(newDate(time));
System.out.println("修改時間[1]"+ctime);
}
}

⑷ 如何用java獲取一個文件的創建時間

java沒有函數能直接拿到文件的創建時間lastModified()只能獲得最後一次修改時間
想要拿到創建時間必須依賴Windows系統的API,通過調CMD命令實現,
代碼如下:
publicstaticvoidmain(String[]args){
try{
Processp=Runtime.getRuntime().exec(
"cmd/Cdird:\test.txt/tc");
InputStreamis=p.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringstr;
inti=0;
while((str=br.readLine())!=null){
i++;
if(i==6){
System.out.println(str.substring(0,17));
}
}

}catch(java.io.IOExceptionexc){
exc.printStackTrace();
}
}

⑸ java編寫程序獲取指定文件的最後修改時間

package com.xian.home2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class FileOperate {

/**
* @param args
*/
public static void main(String[] args) {
// FileOperate fo = new FileOperate();
// String filepath = "G:\\02";
// fo.getFileList(filepath);
getCreateTime();
}

public List<File> getFileList(String filepath) {

try {
File file = new File(filepath);
if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File Ifile = new File(filepath + "\\" + filelist[i]);
FileInputStream fis = new FileInputStream(Ifile);

long modifiedTime = Ifile.lastModified();
Date date=new Date(modifiedTime);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:MM");
String dd=sdf.format(date);
System.out.println("File name:" + Ifile.getName()
+ " \tFile size: "
+ (double) ((double) fis.available() / 1024 / 1024)
+ "M"+ " \tFile create Time: "+dd);

}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;

}

/**
* 讀取文件創建時間
*/
public static void getCreateTime(){
String filePath = "G:\\02\\123.txt";
String strTime = null;
try {
Process p = Runtime.getRuntime().exec("cmd /C dir "
+ filePath
+ "/tc" );
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while((line = br.readLine()) != null){
if(line.endsWith(".txt")){
strTime = line.substring(0,17);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("創建時間 " + strTime);
//輸出:創建時間 2009-08-17 10:21
}
/**
* 讀取文件修改時間的方法1
*/
@SuppressWarnings("deprecation")
public static void getModifiedTime_1(){
File f = new File("C:\\test.txt");
Calendar cal = Calendar.getInstance();
long time = f.lastModified();
cal.setTimeInMillis(time);
//此處toLocalString()方法是不推薦的,但是仍可輸出
System.out.println("修改時間[1] " + cal.getTime().toLocaleString());
//輸出:修改時間[1] 2009-8-17 10:32:38
}

/**
* 讀取修改時間的方法2
*/
public static void getModifiedTime_2(){
File f = new File("C:\\test.txt");
Calendar cal = Calendar.getInstance();
long time = f.lastModified();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(time);
System.out.println("修改時間[2] " + formatter.format(cal.getTime()));
//輸出:修改時間[2] 2009-08-17 10:32:38
}

}

熱點內容
紅點角標演算法 發布:2025-07-12 12:11:16 瀏覽:843
開心消消樂伺服器繁忙什麼情況 發布:2025-07-12 12:11:14 瀏覽:237
資料庫的封鎖協議 發布:2025-07-12 12:10:35 瀏覽:724
如何配置一台長久耐用的電腦 發布:2025-07-12 11:43:03 瀏覽:601
昆明桃源碼頭 發布:2025-07-12 11:38:45 瀏覽:568
大司馬腳本掛機 發布:2025-07-12 11:38:35 瀏覽:458
資料庫實時監控 發布:2025-07-12 11:31:33 瀏覽:743
vb6反編譯精靈 發布:2025-07-12 11:23:12 瀏覽:997
模擬存儲示波器 發布:2025-07-12 11:10:58 瀏覽:814
怎麼查看安卓真實運行內存 發布:2025-07-12 11:08:39 瀏覽:883