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();
}
java file arraylist
New contributor
|
show 2 more comments
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();
}
java file arraylist
New contributor
contactsinformations
isArrayList<String>
so itsget
method already returns String. What is the point of wrapping it withString.valueOf
?
– Pshemo
Nov 18 at 14:19
BTW each time you callnew 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 wrapBufferedWriter
withPrintWriter
which hasprintln
method. For examplePrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
– Pshemo
Nov 18 at 14:27
@Pshemo no need of BufferedWriter betweenPrintWriter 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 addsBufferedWriter
automatically only while handlingOutputStream
, orFile file
, orString file
, but not when handling otherWriter
(although for that case buffering may still be provided by other means which I didn't notice).
– Pshemo
Nov 18 at 14:46
|
show 2 more comments
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();
}
java file arraylist
New contributor
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
java file arraylist
New contributor
New contributor
edited Nov 18 at 18:18
New contributor
asked Nov 18 at 14:09
Ruben Ochoa Perez
83
83
New contributor
New contributor
contactsinformations
isArrayList<String>
so itsget
method already returns String. What is the point of wrapping it withString.valueOf
?
– Pshemo
Nov 18 at 14:19
BTW each time you callnew 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 wrapBufferedWriter
withPrintWriter
which hasprintln
method. For examplePrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
– Pshemo
Nov 18 at 14:27
@Pshemo no need of BufferedWriter betweenPrintWriter 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 addsBufferedWriter
automatically only while handlingOutputStream
, orFile file
, orString file
, but not when handling otherWriter
(although for that case buffering may still be provided by other means which I didn't notice).
– Pshemo
Nov 18 at 14:46
|
show 2 more comments
contactsinformations
isArrayList<String>
so itsget
method already returns String. What is the point of wrapping it withString.valueOf
?
– Pshemo
Nov 18 at 14:19
BTW each time you callnew 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 wrapBufferedWriter
withPrintWriter
which hasprintln
method. For examplePrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Contacts.txt", true)));
– Pshemo
Nov 18 at 14:27
@Pshemo no need of BufferedWriter betweenPrintWriter 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 addsBufferedWriter
automatically only while handlingOutputStream
, orFile file
, orString file
, but not when handling otherWriter
(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
|
show 2 more comments
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.
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 thewriteln()
ofBufferedWriter
, 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
|
show 5 more comments
up vote
0
down vote
As
ArrayList
preserves insertion order you don't need to precise theindex
and you can omit the useless variablesname, surname, telephone
:
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the
ArrayList
isString
you don't need thevalueOf
method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
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);
}
3
Better to useoutStream.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
add a comment |
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());
}
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 aboutString.valueOf()
you are 100% correct. I edited.
– forpas
Nov 18 at 14:25
add a comment |
up vote
0
down vote
outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));
Please add some details description, so that anyone can understand your answer at a glance.
– SkyWalker
Nov 18 at 17:25
add a comment |
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.
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 thewriteln()
ofBufferedWriter
, 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
|
show 5 more comments
up vote
0
down vote
accepted
Use :
outStream.write(contactsinformations.get(i) + "n");
Here n
signifies the new line.
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 thewriteln()
ofBufferedWriter
, 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
|
show 5 more comments
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.
Use :
outStream.write(contactsinformations.get(i) + "n");
Here n
signifies the new line.
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 thewriteln()
ofBufferedWriter
, 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
|
show 5 more comments
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 thewriteln()
ofBufferedWriter
, 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
|
show 5 more comments
up vote
0
down vote
As
ArrayList
preserves insertion order you don't need to precise theindex
and you can omit the useless variablesname, surname, telephone
:
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the
ArrayList
isString
you don't need thevalueOf
method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
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);
}
3
Better to useoutStream.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
add a comment |
up vote
0
down vote
As
ArrayList
preserves insertion order you don't need to precise theindex
and you can omit the useless variablesname, surname, telephone
:
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the
ArrayList
isString
you don't need thevalueOf
method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
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);
}
3
Better to useoutStream.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
add a comment |
up vote
0
down vote
up vote
0
down vote
As
ArrayList
preserves insertion order you don't need to precise theindex
and you can omit the useless variablesname, surname, telephone
:
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the
ArrayList
isString
you don't need thevalueOf
method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
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);
}
As
ArrayList
preserves insertion order you don't need to precise theindex
and you can omit the useless variablesname, surname, telephone
:
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the
ArrayList
isString
you don't need thevalueOf
method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
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);
}
edited Nov 18 at 14:20
answered Nov 18 at 14:13
azro
9,59141337
9,59141337
3
Better to useoutStream.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
add a comment |
3
Better to useoutStream.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
add a comment |
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());
}
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 aboutString.valueOf()
you are 100% correct. I edited.
– forpas
Nov 18 at 14:25
add a comment |
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());
}
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 aboutString.valueOf()
you are 100% correct. I edited.
– forpas
Nov 18 at 14:25
add a comment |
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());
}
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());
}
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 aboutString.valueOf()
you are 100% correct. I edited.
– forpas
Nov 18 at 14:25
add a comment |
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 aboutString.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
add a comment |
up vote
0
down vote
outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));
Please add some details description, so that anyone can understand your answer at a glance.
– SkyWalker
Nov 18 at 17:25
add a comment |
up vote
0
down vote
outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));
Please add some details description, so that anyone can understand your answer at a glance.
– SkyWalker
Nov 18 at 17:25
add a comment |
up vote
0
down vote
up vote
0
down vote
outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));
outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));
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
add a comment |
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
add a comment |
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.
Ruben Ochoa Perez is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
contactsinformations
isArrayList<String>
so itsget
method already returns String. What is the point of wrapping it withString.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
withPrintWriter
which hasprintln
method. For examplePrintWriter 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 handlingOutputStream
, orFile file
, orString file
, but not when handling otherWriter
(although for that case buffering may still be provided by other means which I didn't notice).– Pshemo
Nov 18 at 14:46