BufferedReader和BufferedWriter练习之ArrayList

package com.suning;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class ArrayListDemo {
  public static void main(String[] args) throws IOException {
    arrayListToFile();
    fileToArrayList();
    return;
  }

  public static void arrayListToFile() throws IOException {
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("haha");
    arr.add("helloworld");
    arr.add("thank");
    arr.add("you");

    BufferedWriter bw = new BufferedWriter("arr.txt");
    for (int i = 0; i < arr.size(); i++) {
      bw.write(arr.get(i));
      bw.newLine();
      bw.flush();
    }
    bw.close();
    return;
  }

  public static void fileToArrayList() throws IOException {
    ArrayList<String> arr = new ArrayList<String>();
    BufferedReader br = new BufferedReader(new FileReader("arr.txt"));
    String line;
    while (null != (line = br.readLine())) {
      arr.add(line);
    }
    br.close();

    for (int i = 0; i < arr.size(); i++) {
      System.out.println(arr.get(i));
    }
    return;
  }

}