Storing strings of ArrayList collections to a text file
Analysis:
1, ArrayList collection is stored in a string
2, Traverse ArrayList, get data
3. Store data in a text file
4, by the text file know to use character stream
Code
Package Cn.idcast4;import Java.io.bufferedwriter;import Java.io.filewriter;import java.io.ioexception;import Java.util.arraylist;public class Day12 {public static void main (string[] args) throws IOException {arraylist<string& Gt Array = new arraylist<string> (); Array.add ("Hello"); Array.add ("World"); Array.add ("Java"); BufferedWriter bw = new BufferedWriter (New FileWriter ("A.txt")); for (String S:array) {bw.write (s); Bw.newline (); Bw.flush ();} Bw.close ();}}
Storing data from a text file in the ArrayList collection
Analysis
1. The data source is a text file
2. Destination is a collection
3. The element is a string
Code
Package Cn.idcast4;import Java.io.bufferedreader;import Java.io.filenotfoundexception;import java.io.FileReader; Import Java.io.ioexception;import Java.util.arraylist;public class Day1102 {public static void main (string[] args) Throws IOException {BufferedReader br = new BufferedReader (New FileReader ("B.txt")); arraylist<string> array = new arraylist<string> (); String line = null;while (line = Br.readline ())! = null) {Array.add (line);} Br.close (); for (String S:array) {System.out.println (s);}}}
Storing strings of ArrayList collections in a text file/storing data from a text file in a ArrayList collection