在控制檯中,在eclipse中彈出當前時間戳,並且我可以在它旁邊鍵入任何我想要放入文件的內容。將時間戳添加到文件JAVAR
如何獲得該文件中打印的時間戳!?!?
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();
}
}
}
謝謝生病嘗試一下! – boejosco
當我試圖說我在「寫」下發生錯誤時說 - 方法寫入(int)在BufferedWriter類型不適用於參數(時間戳) – boejosco
嘗試使用'bw.write(new Timestamp(new java.util。 Date().getTime())。toString());' – jiaweizhang