How to check next blank line in textbar and add next input
Hi, I have a problem with my user interface. I currently want my UI to add input to the text bar every time I click save. After pressing the save button, it will return to the menu to ask if the user wants to enter another input. If the user clicks yes, it will return to the input section. However, when I try to type a second time, it will overwrite what I wrote at the beginning. How can I change the code to fix this problem?
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();
}
+3
source to share
3 answers
Thank you all especially for kamikr and Kesavacharan. Below is my edited version. Please feel free to comment if you find that my code could be improved.
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter output = null;
FileInputStream fs = null;
FileWriter fout = 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));
output = new BufferedWriter(new FileWriter(myFile,true));
PrintWriter fileout = new PrintWriter(output,true);
for(int i = 0; i<100; ++i){
String line = br.readLine();
if(line==null){
String name = text1.getText();
String id = text2.getText();
fileout.println(name + " " + id);
break;
}
}
} 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();
}
0
source to share