Java 文件是否存在取决于时间的推移
我试图通过javacode传递特定的时间戳来为文件夹中存在的新文件编码,但我没有得到它..你能帮我吗Java 文件是否存在取决于时间的推移,java,Java,我试图通过javacode传递特定的时间戳来为文件夹中存在的新文件编码,但我没有得到它..你能帮我吗 String filePath = System.getProperty("user.dir"); //create a new file with Time Stamp File file = new File(filePath + "\\" + filename+GetCurrentTimeStamp().replace(":","_").replace(".","_")+"
String filePath = System.getProperty("user.dir");
//create a new file with Time Stamp
File file = new File(filePath + "\\" + filename+GetCurrentTimeStamp().replace(":","_").replace(".","_")+".txt");
try {
if (!file.exists()) {
file.createNewFile();
System.out.println("File is created; file name is " + file.getName());
} else {
System.out.println("File already exist");
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Get current system time
public static String GetCurrentTimeStamp() {
SimpleDateFormat sdfDate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");// dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
// Get Current Host Name
public static String GetCurrentTestHostName() throws UnknownHostException {
InetAddress localMachine = InetAddress.getLocalHost();
String hostName = localMachine.getHostName();
return hostName;
}
// Get Current User Name
public static String GetCurrentTestUserName() {
return System.getProperty("user.name");
我不知道你想描述什么。此外,请详细说明“我不明白”的含义。实际问题是什么?
package com.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class trst {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
boolean b = false;
File file, file1;
Date today = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("MMddyyyyhhmmss");
String folder_name="backup_"+ft.format(today);
file = new File("./"+folder_name);
if (!file.exists())
{
b = file.mkdirs();
}
file1 = new File(file+"/"+"Test");
FileWriter fileWriter = new FileWriter(file1);
fileWriter.write("abc");
fileWriter.flush();
fileWriter.close();
if (b)
System.out.println("Backup has been created.");
else
System.out.println("Failed to create backup.");
}
}