这就是我所拥有的:
try{
String filename = "Names.txt";
FileWriter fw = new FileWriter(filename, true);
BufferedWriter buffer = new BufferedWriter(fw);
buffer.append("NAME: " + name + " AGE: " + age + " ID: " + id + "\n\n");
System.out.println("We have succefully created your account.");
buffer.close();
start();
} catch(IOException e){
System.err.println("ERROR");
}
它总是覆盖第一行而不是另一行。我用过了追加。这是我的开始方法:
// this is the start method
public static void start(){
System.out.println("1) Add Account 2) Exit");
System.out.println("What do you want to do: ");
stuff = input.nextInt();
if (stuff == 1) {
try {
x = new Formatter("Names.txt");
} catch (Exception e) {
System.err.println("ERROR" );
}
newRecord();
} else if(stuff == 2) {
System.exit(0);
} else {
System.err.println("ERROR");
}
}
答案 0 :(得分:3)
我猜是这一行:
x = new Formatter("Names.txt");
来自javadoc
public Formatter(String fileName) throws FileNotFoundException
参数:
fileName - 要用作此格式化程序的目标的文件的名称。 如果该文件存在,那么它将被截断为零大小;否则,将创建一个新文件。输出将被写入文件并被缓冲。
(我强调清理文件的部分。)