Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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());     } 
like image 743
Ricky Avatar asked Jul 01 '11 12:07

Ricky


People also ask

How do you turn an ArrayList into a String?

To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method.

Does ArrayList have a toString?

Note: The ArrayList class does not have its own toString() method. Rather it overrides the method from the Object class.

How do I call an ArrayList from another class after inputting from a text file?

Just define another class and call the setter from your actionPerformed(..) method. Now when you want to access the contents of this list, simply use: ... //any other method ArrayList<String> arraylist = YourOtherClass.


1 Answers

import java.io.FileWriter; ... FileWriter writer = new FileWriter("output.txt");  for(String str: arr) {   writer.write(str + System.lineSeparator()); } writer.close(); 
like image 116
Andrey Adamovich Avatar answered Oct 03 '22 22:10

Andrey Adamovich