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: 
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);

```
If you need to create each ArrayList item in a single line then you can use this code

```
private void createFile(String file, ArrayList<String> arrData)
            throws IOException {
        FileWriter writer = new FileWriter(file + ".txt");
        int size = arrData.size();
        for (int i=0;i<size;i++) {
            String str = arrData.get(i).toString();
            writer.write(str);
            if(i < size-1)**//This prevent creating a blank like at the end of the file**
                writer.write("\n");
        }
        writer.close();
    }

```