Insert line by line data in txt file with arraylist











up vote
1
down vote

favorite












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();
}









share|improve this question









New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
    – Pshemo
    Nov 18 at 14:19










  • BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
    – Pshemo
    Nov 18 at 14:25












  • You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
    – Pshemo
    Nov 18 at 14:27












  • @Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
    – azro
    Nov 18 at 14:27










  • @azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
    – Pshemo
    Nov 18 at 14:46

















up vote
1
down vote

favorite












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();
}









share|improve this question









New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
    – Pshemo
    Nov 18 at 14:19










  • BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
    – Pshemo
    Nov 18 at 14:25












  • You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
    – Pshemo
    Nov 18 at 14:27












  • @Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
    – azro
    Nov 18 at 14:27










  • @azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
    – Pshemo
    Nov 18 at 14:46















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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();
}









share|improve this question









New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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();
}






java file arraylist






share|improve this question









New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 18 at 18:18





















New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 18 at 14:09









Ruben Ochoa Perez

83




83




New contributor




Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ruben Ochoa Perez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
    – Pshemo
    Nov 18 at 14:19










  • BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
    – Pshemo
    Nov 18 at 14:25












  • You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
    – Pshemo
    Nov 18 at 14:27












  • @Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
    – azro
    Nov 18 at 14:27










  • @azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
    – Pshemo
    Nov 18 at 14:46




















  • contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
    – Pshemo
    Nov 18 at 14:19










  • BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
    – Pshemo
    Nov 18 at 14:25












  • You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
    – Pshemo
    Nov 18 at 14:27












  • @Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
    – azro
    Nov 18 at 14:27










  • @azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
    – Pshemo
    Nov 18 at 14:46


















contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
– Pshemo
Nov 18 at 14:19




contactsinformations is ArrayList<String> so its get method already returns String. What is the point of wrapping it with String.valueOf?
– Pshemo
Nov 18 at 14:19












BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
– Pshemo
Nov 18 at 14:25






BTW each time you call new FileWriter("Contacts.txt") it clear content of used file. If you want to append text to already existing file without clearing it take a look at How to add a new line of text to an existing file in Java?
– Pshemo
Nov 18 at 14:25














You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
– Pshemo
Nov 18 at 14:27






You can also wrap BufferedWriter with PrintWriter which has println method. For example PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
– Pshemo
Nov 18 at 14:27














@Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
– azro
Nov 18 at 14:27




@Pshemo no need of BufferedWriter between PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt")); is valid
– azro
Nov 18 at 14:27












@azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
– Pshemo
Nov 18 at 14:46






@azro True, if we don't need buffering provided by BufferedWriter. From what I see ins source code of PrintWriter it adds BufferedWriter automatically only while handling OutputStream, or File file, or String file, but not when handling other Writer (although for that case buffering may still be provided by other means which I didn't notice).
– Pshemo
Nov 18 at 14:46














4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










Use :



outStream.write(contactsinformations.get(i) + "n");


Here n signifies the new line.






share|improve this answer























  • Already tried but output of that is just the first letter
    – Ruben Ochoa Perez
    Nov 18 at 14:12










  • Sorry, did not get that. Could you be more clear?
    – Nicholas K
    Nov 18 at 14:13










  • @TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
    – Nicholas K
    Nov 18 at 14:17










  • Yes. You said n signifies the new line. I use n but the output is the first letter of the name
    – Ruben Ochoa Perez
    Nov 18 at 14:17






  • 1




    I get it now. Thank you.
    – Ruben Ochoa Perez
    Nov 18 at 17:39


















up vote
0
down vote















  1. As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :



    contactsinformations.add(tname.getText());
    contactsinformations.add(tsurname.getText());
    contactsinformations.add(ttelephone.getText());



  2. As the type of the ArrayList is String you don't need the valueOf method :



    for (int i = 0; i < contactsinformations.size(); i++) {
    outStream.write(contactsinformations.get(i));
    outStream.newLine();
    }



  3. The PrintWriter class has a method to do both in one call (combine with for-each loop here) :



    PrintWriter pw = new PrintWriter(outFile);
    for (String contactsinformation : contactsinformations) {
    pw.println(contactsinformation);
    }







share|improve this answer



















  • 3




    Better to use outStream.newLine() rather than writing a hard-coded "n".
    – Ted Hopp
    Nov 18 at 14:16










  • You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
    – Ruben Ochoa Perez
    Nov 18 at 14:20










  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
    – azro
    Nov 18 at 14:22










  • When i use the method 3 i get null
    – Ruben Ochoa Perez
    Nov 18 at 14:31


















up vote
0
down vote













Convert to CharArray and the write BufferedWriter :



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str.toCharArray(), 0, str.length());
}


or write the String by using offset and length the same way as above:



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str, 0, str.length());
}





share|improve this answer























  • Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
    – azro
    Nov 18 at 14:23












  • If your comment was only about String.valueOf() you are 100% correct. I edited.
    – forpas
    Nov 18 at 14:25




















up vote
0
down vote













outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));





share|improve this answer























  • Please add some details description, so that anyone can understand your answer at a glance.
    – SkyWalker
    Nov 18 at 17:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361785%2finsert-line-by-line-data-in-txt-file-with-arraylist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Use :



outStream.write(contactsinformations.get(i) + "n");


Here n signifies the new line.






share|improve this answer























  • Already tried but output of that is just the first letter
    – Ruben Ochoa Perez
    Nov 18 at 14:12










  • Sorry, did not get that. Could you be more clear?
    – Nicholas K
    Nov 18 at 14:13










  • @TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
    – Nicholas K
    Nov 18 at 14:17










  • Yes. You said n signifies the new line. I use n but the output is the first letter of the name
    – Ruben Ochoa Perez
    Nov 18 at 14:17






  • 1




    I get it now. Thank you.
    – Ruben Ochoa Perez
    Nov 18 at 17:39















up vote
0
down vote



accepted










Use :



outStream.write(contactsinformations.get(i) + "n");


Here n signifies the new line.






share|improve this answer























  • Already tried but output of that is just the first letter
    – Ruben Ochoa Perez
    Nov 18 at 14:12










  • Sorry, did not get that. Could you be more clear?
    – Nicholas K
    Nov 18 at 14:13










  • @TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
    – Nicholas K
    Nov 18 at 14:17










  • Yes. You said n signifies the new line. I use n but the output is the first letter of the name
    – Ruben Ochoa Perez
    Nov 18 at 14:17






  • 1




    I get it now. Thank you.
    – Ruben Ochoa Perez
    Nov 18 at 17:39













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Use :



outStream.write(contactsinformations.get(i) + "n");


Here n signifies the new line.






share|improve this answer














Use :



outStream.write(contactsinformations.get(i) + "n");


Here n signifies the new line.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 14:20

























answered Nov 18 at 14:10









Nicholas K

4,04031029




4,04031029












  • Already tried but output of that is just the first letter
    – Ruben Ochoa Perez
    Nov 18 at 14:12










  • Sorry, did not get that. Could you be more clear?
    – Nicholas K
    Nov 18 at 14:13










  • @TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
    – Nicholas K
    Nov 18 at 14:17










  • Yes. You said n signifies the new line. I use n but the output is the first letter of the name
    – Ruben Ochoa Perez
    Nov 18 at 14:17






  • 1




    I get it now. Thank you.
    – Ruben Ochoa Perez
    Nov 18 at 17:39


















  • Already tried but output of that is just the first letter
    – Ruben Ochoa Perez
    Nov 18 at 14:12










  • Sorry, did not get that. Could you be more clear?
    – Nicholas K
    Nov 18 at 14:13










  • @TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
    – Nicholas K
    Nov 18 at 14:17










  • Yes. You said n signifies the new line. I use n but the output is the first letter of the name
    – Ruben Ochoa Perez
    Nov 18 at 14:17






  • 1




    I get it now. Thank you.
    – Ruben Ochoa Perez
    Nov 18 at 17:39
















Already tried but output of that is just the first letter
– Ruben Ochoa Perez
Nov 18 at 14:12




Already tried but output of that is just the first letter
– Ruben Ochoa Perez
Nov 18 at 14:12












Sorry, did not get that. Could you be more clear?
– Nicholas K
Nov 18 at 14:13




Sorry, did not get that. Could you be more clear?
– Nicholas K
Nov 18 at 14:13












@TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
– Nicholas K
Nov 18 at 14:17




@TedHopp : I couldn't find the writeln() of BufferedWriter, is it on a higher java version? I'm on java-8.
– Nicholas K
Nov 18 at 14:17












Yes. You said n signifies the new line. I use n but the output is the first letter of the name
– Ruben Ochoa Perez
Nov 18 at 14:17




Yes. You said n signifies the new line. I use n but the output is the first letter of the name
– Ruben Ochoa Perez
Nov 18 at 14:17




1




1




I get it now. Thank you.
– Ruben Ochoa Perez
Nov 18 at 17:39




I get it now. Thank you.
– Ruben Ochoa Perez
Nov 18 at 17:39












up vote
0
down vote















  1. As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :



    contactsinformations.add(tname.getText());
    contactsinformations.add(tsurname.getText());
    contactsinformations.add(ttelephone.getText());



  2. As the type of the ArrayList is String you don't need the valueOf method :



    for (int i = 0; i < contactsinformations.size(); i++) {
    outStream.write(contactsinformations.get(i));
    outStream.newLine();
    }



  3. The PrintWriter class has a method to do both in one call (combine with for-each loop here) :



    PrintWriter pw = new PrintWriter(outFile);
    for (String contactsinformation : contactsinformations) {
    pw.println(contactsinformation);
    }







share|improve this answer



















  • 3




    Better to use outStream.newLine() rather than writing a hard-coded "n".
    – Ted Hopp
    Nov 18 at 14:16










  • You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
    – Ruben Ochoa Perez
    Nov 18 at 14:20










  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
    – azro
    Nov 18 at 14:22










  • When i use the method 3 i get null
    – Ruben Ochoa Perez
    Nov 18 at 14:31















up vote
0
down vote















  1. As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :



    contactsinformations.add(tname.getText());
    contactsinformations.add(tsurname.getText());
    contactsinformations.add(ttelephone.getText());



  2. As the type of the ArrayList is String you don't need the valueOf method :



    for (int i = 0; i < contactsinformations.size(); i++) {
    outStream.write(contactsinformations.get(i));
    outStream.newLine();
    }



  3. The PrintWriter class has a method to do both in one call (combine with for-each loop here) :



    PrintWriter pw = new PrintWriter(outFile);
    for (String contactsinformation : contactsinformations) {
    pw.println(contactsinformation);
    }







share|improve this answer



















  • 3




    Better to use outStream.newLine() rather than writing a hard-coded "n".
    – Ted Hopp
    Nov 18 at 14:16










  • You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
    – Ruben Ochoa Perez
    Nov 18 at 14:20










  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
    – azro
    Nov 18 at 14:22










  • When i use the method 3 i get null
    – Ruben Ochoa Perez
    Nov 18 at 14:31













up vote
0
down vote










up vote
0
down vote











  1. As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :



    contactsinformations.add(tname.getText());
    contactsinformations.add(tsurname.getText());
    contactsinformations.add(ttelephone.getText());



  2. As the type of the ArrayList is String you don't need the valueOf method :



    for (int i = 0; i < contactsinformations.size(); i++) {
    outStream.write(contactsinformations.get(i));
    outStream.newLine();
    }



  3. The PrintWriter class has a method to do both in one call (combine with for-each loop here) :



    PrintWriter pw = new PrintWriter(outFile);
    for (String contactsinformation : contactsinformations) {
    pw.println(contactsinformation);
    }







share|improve this answer
















  1. As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :



    contactsinformations.add(tname.getText());
    contactsinformations.add(tsurname.getText());
    contactsinformations.add(ttelephone.getText());



  2. As the type of the ArrayList is String you don't need the valueOf method :



    for (int i = 0; i < contactsinformations.size(); i++) {
    outStream.write(contactsinformations.get(i));
    outStream.newLine();
    }



  3. The PrintWriter class has a method to do both in one call (combine with for-each loop here) :



    PrintWriter pw = new PrintWriter(outFile);
    for (String contactsinformation : contactsinformations) {
    pw.println(contactsinformation);
    }








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 14:20

























answered Nov 18 at 14:13









azro

9,59141337




9,59141337








  • 3




    Better to use outStream.newLine() rather than writing a hard-coded "n".
    – Ted Hopp
    Nov 18 at 14:16










  • You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
    – Ruben Ochoa Perez
    Nov 18 at 14:20










  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
    – azro
    Nov 18 at 14:22










  • When i use the method 3 i get null
    – Ruben Ochoa Perez
    Nov 18 at 14:31














  • 3




    Better to use outStream.newLine() rather than writing a hard-coded "n".
    – Ted Hopp
    Nov 18 at 14:16










  • You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
    – Ruben Ochoa Perez
    Nov 18 at 14:20










  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
    – azro
    Nov 18 at 14:22










  • When i use the method 3 i get null
    – Ruben Ochoa Perez
    Nov 18 at 14:31








3




3




Better to use outStream.newLine() rather than writing a hard-coded "n".
– Ted Hopp
Nov 18 at 14:16




Better to use outStream.newLine() rather than writing a hard-coded "n".
– Ted Hopp
Nov 18 at 14:16












You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
– Ruben Ochoa Perez
Nov 18 at 14:20




You mean by the first to use it as result this way: ArrayList<String> contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" ");
– Ruben Ochoa Perez
Nov 18 at 14:20












@RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
– azro
Nov 18 at 14:22




@RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line
– azro
Nov 18 at 14:22












When i use the method 3 i get null
– Ruben Ochoa Perez
Nov 18 at 14:31




When i use the method 3 i get null
– Ruben Ochoa Perez
Nov 18 at 14:31










up vote
0
down vote













Convert to CharArray and the write BufferedWriter :



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str.toCharArray(), 0, str.length());
}


or write the String by using offset and length the same way as above:



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str, 0, str.length());
}





share|improve this answer























  • Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
    – azro
    Nov 18 at 14:23












  • If your comment was only about String.valueOf() you are 100% correct. I edited.
    – forpas
    Nov 18 at 14:25

















up vote
0
down vote













Convert to CharArray and the write BufferedWriter :



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str.toCharArray(), 0, str.length());
}


or write the String by using offset and length the same way as above:



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str, 0, str.length());
}





share|improve this answer























  • Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
    – azro
    Nov 18 at 14:23












  • If your comment was only about String.valueOf() you are 100% correct. I edited.
    – forpas
    Nov 18 at 14:25















up vote
0
down vote










up vote
0
down vote









Convert to CharArray and the write BufferedWriter :



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str.toCharArray(), 0, str.length());
}


or write the String by using offset and length the same way as above:



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str, 0, str.length());
}





share|improve this answer














Convert to CharArray and the write BufferedWriter :



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str.toCharArray(), 0, str.length());
}


or write the String by using offset and length the same way as above:



for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "n";
outStream.write(str, 0, str.length());
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 15:25

























answered Nov 18 at 14:21









forpas

4,1861216




4,1861216












  • Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
    – azro
    Nov 18 at 14:23












  • If your comment was only about String.valueOf() you are 100% correct. I edited.
    – forpas
    Nov 18 at 14:25




















  • Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
    – azro
    Nov 18 at 14:23












  • If your comment was only about String.valueOf() you are 100% correct. I edited.
    – forpas
    Nov 18 at 14:25


















Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
– azro
Nov 18 at 14:23






Please think about it before posting your answer, look at the other one if you don't think to all by yourself : valufOf is 100% useless and what it's the gain of write the method instead of the classic with String only ?
– azro
Nov 18 at 14:23














If your comment was only about String.valueOf() you are 100% correct. I edited.
– forpas
Nov 18 at 14:25






If your comment was only about String.valueOf() you are 100% correct. I edited.
– forpas
Nov 18 at 14:25












up vote
0
down vote













outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));





share|improve this answer























  • Please add some details description, so that anyone can understand your answer at a glance.
    – SkyWalker
    Nov 18 at 17:25















up vote
0
down vote













outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));





share|improve this answer























  • Please add some details description, so that anyone can understand your answer at a glance.
    – SkyWalker
    Nov 18 at 17:25













up vote
0
down vote










up vote
0
down vote









outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));





share|improve this answer














outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 16:09









Nicholas K

4,04031029




4,04031029










answered Nov 18 at 14:14









Ilya

1728




1728












  • Please add some details description, so that anyone can understand your answer at a glance.
    – SkyWalker
    Nov 18 at 17:25


















  • Please add some details description, so that anyone can understand your answer at a glance.
    – SkyWalker
    Nov 18 at 17:25
















Please add some details description, so that anyone can understand your answer at a glance.
– SkyWalker
Nov 18 at 17:25




Please add some details description, so that anyone can understand your answer at a glance.
– SkyWalker
Nov 18 at 17:25










Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.













Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.












Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361785%2finsert-line-by-line-data-in-txt-file-with-arraylist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin