[英]Why does FileWriter overwrite the file even though I did specify it should append
这就是我所拥有的:
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");
}
}
我猜是这行:
x = new Formatter("Names.txt");
public Formatter(String fileName) throws FileNotFoundException
参数:
fileName-用作此格式化程序目标的文件名。 如果文件存在,它将被截断为零 ; 否则,将创建一个新文件。 输出将被写入文件并被缓冲。
(我将重点放在清理文件的部分)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.