1

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();
                 }
  • `contactsinformations` is `ArrayList` so its `get` method already returns String. What is the point of wrapping it with `String.valueOf`? – Pshemo Nov 18 '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?](https://stackoverflow.com/q/4614227) – Pshemo Nov 18 '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 '18 at 14:27
  • @Pshemo no need of BufferedWriter between `PrintWriter pw = new PrintWriter(new FileWriter("Contacts.txt"));` is valid – azro Nov 18 '18 at 14:27
  • @azro True, if we don't need buffering provided by BufferedWriter. From what I see ins [source code of PrintWriter](https://zgrepcode.com/java/oracle/jdk-8u181/java/io/printwriter.java) 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 '18 at 14:46
  • It works but there is no another way of use BufferedWriter? –  Nov 18 '18 at 15:11
  • Forget what i said. Guys it works! just use FileWriter outFile = new FileWriter("Contacts.txt",true); Questin has been updated –  Nov 18 '18 at 18:14

4 Answers4

0

Use :

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

Here \n signifies the new line.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • @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 '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 –  Nov 18 '18 at 14:17
  • No its just empty –  Nov 18 '18 at 14:30
  • Use a debugger to see if the correct values are being assigned to the list. Adding a new line after that is a piece of cake. – Nicholas K Nov 18 '18 at 14:31
  • he output is letter null letter by line –  Nov 18 '18 at 14:41
  • Oh so that means the new line works fine. Now the issue is that the right values aren't getting set to your array list. You need to trace out the reason behind that by, indeed, more debugging :-) – Nicholas K Nov 18 '18 at 15:13
  • 1
    I get it now. Thank you. –  Nov 18 '18 at 17:39
0
  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);
    }
    
azro
  • 53,056
  • 7
  • 34
  • 70
  • 3
    Better to use `outStream.newLine()` rather than writing a hard-coded `"\n"`. – Ted Hopp Nov 18 '18 at 14:16
  • You mean by the first to use it as result this way: ArrayList contactsinformations=new ArrayList<>(); contactsinformations.add(tname.getText()+" "); contactsinformations.add(tsurname.getText()+" "); contactsinformations.add(ttelephone.getText()+" "); –  Nov 18 '18 at 14:20
  • @RubenOchoaPerez I've edit, no need of +" " as it's at the end of the line – azro Nov 18 '18 at 14:22
  • When i use the method 3 i get null –  Nov 18 '18 at 14:31
0
outStream.write(contactsinformations.stream()
         .collect(Collectors.joining(System.lineSeparator())));
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Ilya
  • 720
  • 6
  • 14
0

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()); 
}
forpas
  • 160,666
  • 10
  • 38
  • 76
  • 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 '18 at 14:23
  • If your comment was only about `String.valueOf()` you are 100% correct. I edited. – forpas Nov 18 '18 at 14:25