How to write an ArrayList of Strings into a text file?

Java

Java Problem Overview


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());	
}

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

You can do that with a single line of code nowadays. Create the arrayList and the Path object representing the file where you want to write into:

Path out = Paths.get("output.txt");
List<String> arrayList = new ArrayList<> ( Arrays.asList ( "a" , "b" , "c" ) );

Create the actual file, and fill it with the text in the ArrayList:

Files.write(out,arrayList,Charset.defaultCharset());

Solution 3 - Java

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

Solution 4 - Java

If you want to serialize the ArrayList object to a file so you can read it back in again later use ObjectOuputStream/ObjectInputStream writeObject()/readObject() since ArrayList implements Serializable. It's not clear to me from your question if you want to do this or just write each individual item. If so then Andrey's answer will do that.

Solution 5 - Java

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();
	}

Solution 6 - Java

You might use ArrayList overloaded method toString()

String tmp=arr.toString();
PrintWriter pw=new PrintWriter(new FileOutputStream(file));
pw.println(tmp.substring(1,tmp.length()-1));

Solution 7 - Java

I think you can also use BufferedWriter :

BufferedWriter writer = new BufferedWriter(new FileWriter(new File("note.txt")));

String stuffToWrite = info;

writer.write(stuffToWrite);

writer.close();

and before that remember too add

import java.io.BufferedWriter;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRickyView Question on Stackoverflow
Solution 1 - JavaAndrey AdamovichView Answer on Stackoverflow
Solution 2 - JavagabinetexView Answer on Stackoverflow
Solution 3 - Javachao_changView Answer on Stackoverflow
Solution 4 - JavaribramView Answer on Stackoverflow
Solution 5 - JavaJoseph SelvarajView Answer on Stackoverflow
Solution 6 - JavastemmView Answer on Stackoverflow
Solution 7 - JavaSahar View Answer on Stackoverflow