how to array list with a delimiter into text file java

how to array list with a delimiter into text file java

To write an ArrayList to a text file with a delimiter in Java, you can use the BufferedWriter class. Here's an example of how to do this:

ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");

// Set the delimiter
String delimiter = ",";

try (BufferedWriter writer = new BufferedWriter(new FileWriter("list.txt"))) {
    for (String item : list) {
        writer.write(item);
        writer.write(delimiter);
    }
} catch (IOException e) {
    e.printStackTrace();
}
Source:w‮ual.ww‬tturi.com

This will write the contents of the ArrayList to the file list.txt, with the delimiter "," between each element. The resulting file will look like this:

apple,banana,cherry,

You can use a different delimiter by simply replacing the delimiter variable with the desired delimiter.

Created Time:2017-11-01 12:05:11  Author:lautturi