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()); }
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.
Note: The ArrayList class does not have its own toString() method. Rather it overrides the method from the Object class.
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.
import java.io.FileWriter; ... FileWriter writer = new FileWriter("output.txt"); for(String str: arr) { writer.write(str + System.lineSeparator()); } writer.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With