我的应用程序需要记录应用程序将消息输出到名为" profile_admin_ddmmyyyy(其中ddmmyyyy是当前日期)的文件。使用以下代码从当前目录中提取路径:
FileInputStream propFile = new FileInputStream("config.ini");
config.load(propFile);
path = config.getProperty("path");
在我的ini中,我有"路径"为:
path=C:\app27\bin\profile_admin_
我需要能够将日期附加到此文件的末尾,文件类型为" .log"。任何人都可以帮我实现这个目标吗?
我已经包含了用于编写日志文件的Class I调用:
private String path;
private boolean appendToFile = false;
public FunctionLogging(String file_path, boolean append_value)
{
path = file_path;
appendToFile = append_value;
}
public void writeToFile(String textLine)
{
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat();
format = new SimpleDateFormat("dd-MM-yyyy|HH:mm:ss:ms|");
String timeStamp = format.format(date);
try
{
FileInputStream propFile = new FileInputStream("config.ini");
Properties config = new Properties(System.getProperties());
config.load(propFile);
path = config.getProperty("path");
FileWriter write = new FileWriter(path, appendToFile);
PrintWriter printlines = new PrintWriter(write);
printlines.printf("%s" + "%n"+ timeStamp, textLine);
printlines.close();
}
catch(IOException e)
{
e.printStackTrace();
}
答案 0 :(得分:1)
您需要创建一个单独的SimpleDateFormat实例,以格式化您要在文件名中使用的格式的日期(" ddMMyyyy")。然后将其结果附加到您要打开的文件的路径:
FileWriter write = new FileWriter(path + timestamp + ".log", appendToFile);