Java如何把list数据导入txt文件?

2024-11-22 21:08:18
推荐回答(3个)
回答1:

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

public class Test {
public static void main(String[] args) throws IOException {
List list=new ArrayList();
for (int i = 0; i < 10; i++) {
list.add(i);//集合中放入0-9
}
//下面是写文件
boolean flag = false;
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d:/test.txt", true);
bw = new BufferedWriter(fw, 100);
for (Integer integer : list) {
bw.write(integer+"");
}

flag = true;
} catch (IOException e) {
System.out.println("写入文件出错");
flag = false;
} finally {
if (bw != null) {
bw.flush();
bw.close();
}
if (fw != null)
fw.close();
}
}
}

回答2:

jasperreport

回答3:

file输入输出流
fileinputstream