将时间戳添加到文件 JAVA
Add Timestamp to file JAVA
在控制台中,在 eclipse 中,弹出当前时间戳,我可以在它旁边键入我想要放入文件中的任何内容。
如何让时间戳也打印在文件中!?!?
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.sql.Timestamp;
import java.util.Date;
public class bufferedwriter {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
String lineToPrint = "";
String fileName = "/Users/josephbosco/fileName.txt";
do{
java.util.Date date= new java.util.Date();
System.out.print(new Timestamp(date.getTime()));
lineToPrint = myScanner.nextLine();
printToFile (fileName, lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );
}
public static void printToFile (String myfileName, String message) {
try {
File outfile = new File(myfileName);
//if file doesn't exist, then create it
if (!outfile.exists()) {
System.out.println("No file exists...writing a new file");
outfile.createNewFile();
}
FileWriter fw = new FileWriter(outfile.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(message);
bw.flush();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
每次调用 bw.write(message);
只需添加以下内容:
bw.write(new Timestamp(new java.util.Date().getTime()).toString());
您的代码当前正在 print 语句中实例化一个新的 Timestamp 对象。问题是您没有将该时间戳存储到变量中,以便在尝试将其写入文件时可以再次引用它。
do{
java.util.Date date= new java.util.Date();
System.out.print(new Timestamp(date.getTime()));
lineToPrint = myScanner.nextLine();
printToFile (fileName, lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );
将 Timestamp 对象存储到变量中允许您在打印语句中引用该变量;这也使得 timestamp 变量和 lineToPrint 变量的连接更容易编码。下面修改后的代码显示了这些更改。
do{
java.util.Date date= new java.util.Date();
// Initialize variable and store new Timestamp object
Timestamp timestamp = new Timestamp(date.getTime()));
System.out.print(timestamp)
lineToPrint = myScanner.nextLine();
// Concatenate the two variables
printToFile (fileName, timestamp + " " + lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );
在控制台中,在 eclipse 中,弹出当前时间戳,我可以在它旁边键入我想要放入文件中的任何内容。
如何让时间戳也打印在文件中!?!?
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.sql.Timestamp;
import java.util.Date;
public class bufferedwriter {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
String lineToPrint = "";
String fileName = "/Users/josephbosco/fileName.txt";
do{
java.util.Date date= new java.util.Date();
System.out.print(new Timestamp(date.getTime()));
lineToPrint = myScanner.nextLine();
printToFile (fileName, lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );
}
public static void printToFile (String myfileName, String message) {
try {
File outfile = new File(myfileName);
//if file doesn't exist, then create it
if (!outfile.exists()) {
System.out.println("No file exists...writing a new file");
outfile.createNewFile();
}
FileWriter fw = new FileWriter(outfile.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(message);
bw.flush();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
每次调用 bw.write(message);
只需添加以下内容:
bw.write(new Timestamp(new java.util.Date().getTime()).toString());
您的代码当前正在 print 语句中实例化一个新的 Timestamp 对象。问题是您没有将该时间戳存储到变量中,以便在尝试将其写入文件时可以再次引用它。
do{
java.util.Date date= new java.util.Date();
System.out.print(new Timestamp(date.getTime()));
lineToPrint = myScanner.nextLine();
printToFile (fileName, lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );
将 Timestamp 对象存储到变量中允许您在打印语句中引用该变量;这也使得 timestamp 变量和 lineToPrint 变量的连接更容易编码。下面修改后的代码显示了这些更改。
do{
java.util.Date date= new java.util.Date();
// Initialize variable and store new Timestamp object
Timestamp timestamp = new Timestamp(date.getTime()));
System.out.print(timestamp)
lineToPrint = myScanner.nextLine();
// Concatenate the two variables
printToFile (fileName, timestamp + " " + lineToPrint);
} while (!lineToPrint.equalsIgnoreCase("q") );