My program is about contacts. When a Doctor for an example insert name,surname,telephone, everytime he wants to save the data into txt file then output will be:
somename
somesurname
sometelephone
somename
somesurname
sometelephone
...
Right now i did the output will be only in one line:
somename somesurname sometelephone
as you can see at the code:
if(text.equals("Save")) {
try {
ArrayList<String> contactsinformations=new ArrayList<>();
String name=tname.getText();
String surname=tsurname.getText();
String telephone=ttelephone.getText();
contactsinformations.add(0,name+" ");
contactsinformations.add(1,surname+" ");
contactsinformations.add(2,telephone+" ");
FileWriter outFile = new FileWriter("Contacts.txt");
BufferedWriter outStream = new BufferedWriter(outFile);
for(int i=0; i<contactsinformations.size(); i++)
outStream.write(String.valueOf(contactsinformations.get(i)));
outStream.close();
JOptionPane.showMessageDialog(this,"Data saved.");
} catch(IOException e) {
System.out.println("ERROR IN FILE");
}
}
I use for loop to get the size of the ArrayList but trying to figure out how can I insert the informations in different line.
WARNING: UPDATED QUESTION! Question solved with just a true!
if(text.equals("Save")) {
try
{
ArrayList<String> contactsinformations=new ArrayList<>();
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
FileWriter outFile = new FileWriter("Contacts.txt",true);
BufferedWriter outStream = new BufferedWriter(outFile);
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
JOptionPane.showMessageDialog(this,"Data saved.");
outStream.close();
}