【发布时间】:2013-11-22 20:50:17
【问题描述】:
我正在使用PrintWriter 类将新行写入文件。它按预期写入行,但是当我想写入新数据时,它会覆盖旧数据而不是写入新行。这是我目前所拥有的。
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.io.*;
public class TimeTracker
{
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException
{
DecimalFormat money = new DecimalFormat("$#,##0.00");
PrintWriter toFile = new PrintWriter("timeWorked.dat");
Scanner readFile = new Scanner(new FileReader("timeWorked.dat"));
String client = JOptionPane.showInputDialog("Client's Name");
int rate = Integer.parseInt(JOptionPane.showInputDialog("Rate per hour?"));
String output;
output = "Client's Name: " + client +
" Pay rate agreed: " + money.format(rate) + " per hour";
toFile.append(output);
toFile.flush();
if (readFile.hasNext())
{
toFile.append("\n");
toFile.write(output);
toFile.flush();
}
JOptionPane.showMessageDialog(null, readFile.nextLine());
}
}
【问题讨论】:
-
您同时读写同一个文件?故意的?
-
是的,我做了 readFile 来检查文件是否有一行,如果有,我希望它写一个新行而不覆盖旧行,但不能让它工作。跨度>
标签: java printwriter