//输出结果为csv文件
public static void Text2csv(List<String> list) throws Exception {
// 如果该目录下不存在该文件,则文件会被创建到指定目录下。如果该目录有同名文件,那么该文件将被覆盖。
String FilePath = "result.csv";
File file = new File(FilePath);
if (!file.exists()) {
file.createNewFile();
}
try {
//通过BufferedReader类创建一个使用默认大小输出缓冲区的缓冲字符输出流
BufferedWriter writeText = new BufferedWriter(new FileWriter(FilePath, true));
//调用write的方法将字符串写到流中
for (String text : list) {
writeText.append(text);
writeText.newLine(); //换行
}
writeText.flush();
writeText.close();
} catch (FileNotFoundException e) {
System.out.println("没有找到指定文件");
} catch (IOException e) {
System.out.println("文件读写出错");
}
}
java如何将结果保存到csv文件中
最新推荐文章于 2024-07-16 05:17:18 发布
本文介绍了一个简单的Java程序示例,演示如何将List集合中的字符串数据写入CSV文件。程序首先检查目标文件是否存在,如果不存在则创建新文件;若已存在,则直接使用。通过BufferedWriter进行数据写入,每条记录后自动换行。
662

被折叠的 条评论
为什么被折叠?



