import org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TestIo {
//路径
private static final String PATH = "D:\\demo1.txt";
@Test
public void testIo() throws IOException {
Listlist = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
File file = new File(PATH);
//如果没有文件就创建
if (!file.isFile()) {
file.createNewFile();
}
//
BufferedWriter writer = new BufferedWriter(new FileWriter(PATH));
for (String l:list){
writer.write(l);
}
writer.close();
System.out.println("完成");
}
}
结果
希望对你有帮助
ObjectOutputStresm out=new ObjectOutputStream(new FileOutputStream(new File("your.txt")));
Object o=list.get(i);
out.write(o);
out.close();
你可以到“我爱编程”贴吧去提问 有吧友会给你解答的
读取list集合里的内容不用io吧
楼上那直接写对象写进文件也行?遍历集合 写你所需要的内容呗