嗨,我的UI出现问题。当前,我希望我的UI每次单击保存时都将输入追加到文本板中。单击保存按钮后,它将返回菜单,询问用户是否要输入其他输入。如果用户单击“是”,它将返回到输入部分。但是,当我第二次尝试输入时,它将覆盖我最初编写的内容。如何更改我的代码以解决此问题?
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
try {
// TODO add your handling code here:
File myFile = new File("C:/Users/kai/Desktop/sample.txt");
fs = new FileInputStream("C:/Users/kai/Desktop/sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
myFile.createNewFile();
output = new BufferedWriter(new FileWriter(myFile));
//output.flush();
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line.equals(null)){
String name = text1.getText();
String id = text2.getText();
output.write(name + " " + id);
break;
}
}
// output.newLine();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.dispose();
}
但是,当我第二次尝试输入时,它将覆盖我最初编写的内容。
打开FileWriter
追加模式。
阅读FileWriter
API以找到要使用的适当构造函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句