Question: I want to write an `ArrayList<String>` into a text file. 

The `ArrayList` is created with the code:

```
ArrayList arr = new ArrayList();

StringTokenizer st = new StringTokenizer(
    line, ":Mode set - Out of Service In Service");

while(st.hasMoreTokens()){
    arr.add(st.nextToken());    
}

```

Answer: 
```
import java.io.FileWriter;
...
FileWriter writer = new FileWriter("output.txt"); 
for(String str: arr) {
  writer.write(str + System.lineSeparator());
}
writer.close();

```
I would suggest using FileUtils from Apache Commons IO library.It will create the parent folders of the output file,if they don't exist.while `Files.write(out,arrayList,Charset.defaultCharset());` will not do this,throwing exception if the parent directories don't exist.

```
FileUtils.writeLines(new File("output.txt"), encoding, list);

```