0

I have to print a Array List in a text file. I need a output in separate line but I got in same line.

Example: My expected output in text file: bat ball print my actual output is : [bat,ball,print]

my console it print line by line anyone help me to out of this issue thank you

Syed Ali
  • 5
  • 4

5 Answers5

2

It would be helpful if you could give your code. But here is the code assuming strings are in ArrayList

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;

public class TestJava{
    public static void main(String[] args){
        ArrayList<String> values = new ArrayList<>();
        values.add("bat");
        values.add("ball");
        values.add("print");
        BufferedWriter wr = null;
        try {
            wr = new BufferedWriter(new FileWriter("output.txt"));
            for (String var : values) {
                wr.write(var);
                wr.newLine();
            }
            wr.close();

        } catch (Exception e) {
            //TODO: handle exception
        }

    }
}
kadekhar
  • 71
  • 3
0

You can use fout.writeChars() everytime, you call this a new line will be printed. Just loop through your list and print one by one.

try{

DataOutputStream fout = new DataOutputStream(new BufferedOutputStream (new FileOutputStream("Lines.Txt")));
fout.writeChars("This is first line");
fout.writeChars("This is second line");
fout.close();

}
catch (Exception e){
  e.printStackTrace();
  System.out.println("Exception occured..");
}
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
0

I hope this sample code is working.

List<String> sample = new ArrayList<>();
    sample.add("sample");
    sample.add("sample1");
    sample.add("sample2");
    sample.add("sample3");
    ;
    System.out.println(sample.stream().map(Object::toString)
            .collect(Collectors.joining("\n")));
olgunyldz
  • 531
  • 3
  • 8
0

You can do something like:

public static void main(String[] args) {
String data= null;
List<String> test = new ArrayList<>();
test.add("test");
test.add("test1");
test.add("test2");
test.add("test3");

    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME));
        for(String testList : test) {
        data = testList;

        bw.write(data);
        bw.newLine();
        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {
    bw.close();
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
0

Here is a working solution to what I understood your problem was:

        List<String> sample = new ArrayList<>();
        sample.add("foo");
        sample.add("bar");
        sample.add("bizz");
        sample.add("bazz");

        BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt"));
        sample.stream().forEach(s -> {
            System.out.println(s);
            try {
                writer.write(s + '\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        writer.close();

First it adds some strings to the list. The stream and forEach are the functional way to do iteration nowadays. Then you write to file.

RicardoE
  • 1,665
  • 6
  • 24
  • 42