【把 ArrayList 集合中的字符串内容写到文本文件中】

时间:2022-06-13 20:16:39
package com.companyname.common.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Description 把 ArrayList 集合中的字符串内容写到文本文件中
 * @Author Created by shusheng.
 * @Email shusheng@yiji.com
 * @Date 2018/12/2
 */
public class ArrayListToFileDemo {

    public static void main(String[] args) throws IOException {

        ArrayList<String> 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();
    }

}