public static void main(String[] args) {
//txt文件生成的位置
File outFile = new File("D:\\ce\\test.txt");
//集合数据
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
Writer out;
try {
//输入流,进行写入操作
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile, true), "utf-8"), 10240);
for (int i = 0; i < list.size(); i++) {
//开始写
out.write(
"测试第" + i + "行:" + list.get(i));
out.write("\r\n");
}
//清空缓存区,完全写入
out.flush();
//关闭流
out.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
结果
|