0

我有一个日期值,我想使用它写入文件

 fw = new BufferedWriter(new FileWriter(root + "/log.txt"));

             fw.newLine();
             fw.write("," + "server timestamp   " + "," +  " Timestamp");
             fw.newLine();
             fw.write( " ,");
             fw.write(dateString + "," +  currentTimeStamp);

但每次 dateString 和 currentTimeStamp 为空,通过 toast 我可以看到 dateString 和 currentTimeStamp 不为空,但我无法通过 FileWritter 编写它们。

这是 dateString 的代码

SimpleDateFormat formatter = new SimpleDateFormat("HH-mm-ss_SSS");


             dateString = formatter.format(returnTime);


                 currentTimeStamp = formatter.format(new Date());
4

3 回答 3

0

One issue is that you never define returnTime

于 2013-08-18T02:29:21.380 回答
0

The variables were not initialized correctly.

于 2013-08-18T02:44:06.100 回答
0

我运行你的代码

String dateString, currentTimeStamp;
SimpleDateFormat formatter = new SimpleDateFormat("HH-mm-ss_SSS");
Date returnTime = new Date();

dateString = formatter.format(returnTime);
currentTimeStamp = formatter.format(new Date());

BufferedWriter fw = new BufferedWriter(new FileWriter("E:\\log.txt"));
fw.newLine();
fw.write("," + "server timestamp   " + "," + " Timestamp");
fw.newLine();
fw.write(" ,");
fw.write(dateString + "," + currentTimeStamp);
fw.flush();
fw.close();

我得到 log.txt

在此处输入图像描述

于 2013-08-18T02:44:59.723 回答