java 导出csv类型文件

这篇博客介绍了如何使用Java进行CSV文件的导出操作。通过一个`exportCsv()`方法,将数据列表转换并写入CSV文件。代码示例中创建了一个ArrayList,包含字符串数据,然后调用静态方法`exportCsv()`,传入文件路径和数据列表,成功导出后返回布尔值表示操作状态。
public void exportCsv() {
   List<String> dataList=new ArrayList<String>();
   dataList.add("数据1,数据2,数据3");
   boolean isSuccess=exportCsv(""D://, dataList);
   System.out.println("csv"+isSuccess);
}

/**
 * 导出
 *
 * @param file csv文件(路径+文件名),csv文件不存在会自动创建
 * @param dataList 数据
 * @return
 */
public static boolean exportCsv(File file, List<String> dataList){
   boolean isSucess=false;
   FileOutputStream out=null;
   OutputStreamWriter osw=null;
   BufferedWriter bw=null;
   try {
      out = new FileOutputStream(file);
      osw = new OutputStreamWriter(out);
      bw =new BufferedWriter(osw);
      if(dataList!=null && !dataList.isEmpty()){
         for(String data : dataList){
            bw.append(data).append("\r");
         }
      }
      isSucess=true;
   } catch (Exception e) {
      isSucess=false;
   }finally{
      if(bw!=null){
         try {
            bw.close();
            bw=null;
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      if(osw!=null){
         try {
            osw.close();
            osw=null;
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      if(out!=null){
         try {
            out.close();
            out=null;
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
   return isSucess;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值