美文网首页
BufferedReader和BufferedWriter练习之

BufferedReader和BufferedWriter练习之

作者: March_Cullen | 来源:发表于2018-11-06 23:14 被阅读0次
    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;
      }
    
    }
    
    

    相关文章

      网友评论

          本文标题:BufferedReader和BufferedWriter练习之

          本文链接:https://www.haomeiwen.com/subject/dpgpxqtx.html