我有以下代码来初始化一个printwriter对象 -
/* This function is used to initialise the printwriter element so that it can begin the task of writing data into assignor.txt file...
*
*/
public void startwriterassignor(String filename, boolean appendToFile) {
//pw = null;
try
{
if (appendToFile== true)
{
//If the file already exists, start writing at the end of it.
pw = new PrintWriter(new FileWriter(filename, true));
}
else {
pw = new PrintWriter(new FileWriter(filename, false));
// this is equal to:
// pw = new PrintWriter(new FileWriter(filename, false));
}
//pw.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
首先,我使用下面的调用调用上述函数 -
startwriterassignor("assignor.txt", false);
在将一些数据写入文件后,我再次使用下面的调用调用相同的函数 -
startwriterassignor("assignor.txt", true);
第二次调用'startiwriterassignor'后,会将更多数据写入(追加)到文件中。但是,新数据未附加到文件assignor.txt,我该如何纠正此错误?