【转】Java操作CSV文件导入导出

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处: http://www.cnblogs.com/mao2080/
 1 public class CSVUtils {
 2     
 3     /**
 4      * 
 5      * 描述:导出
 6      * @author mao2080@sina.com
 7      * @created 2017年8月26日 下午2:39:13
 8      * @since 
 9      * @param file csv文件(路径+文件名),csv文件不存在会自动创建
10      * @param dataList 数据(data1,data2,data3...)
11      * @return
12      */
13     public static boolean exportCsv(File file, List<String> dataList){
14         FileOutputStream out= null;
15         OutputStreamWriter osw = null;
16         BufferedWriter bfw= null;
17         try {
18             out = new FileOutputStream(file);
19             osw = new OutputStreamWriter(out, "gbk");
20             bfw = new BufferedWriter(osw);
21             if(dataList != null && !dataList.isEmpty()){
22                 for(String data : dataList){
23                     bfw.append(data).append("\r");
24                 }
25             }
26             return true;
27         } catch (Exception e) {
28             return false;
29         }finally{
30             IOUtil.closeQuietly(bfw, osw, out);
31         }
32     }
33     
34     /**
35      * 
36      * 描述:导入
37      * @author mao2080@sina.com
38      * @created 2017年8月26日 下午2:42:08
39      * @since 
40      * @param file csv文件(路径+文件名)
41      * @return
42      */
43     public static List<String> importCsv(File file){
44         List<String> dataList = new ArrayList<String>();
45         BufferedReader br = null;
46         try { 
47             br = new BufferedReader(new FileReader(file));
48             String line = "";
49             while ((line = br.readLine()) != null) { 
50                 dataList.add(line);
51             }
52         }catch (Exception e) {
53             
54         }finally{
55             IOUtil.closeQuietly(br);
56         }
57         return dataList;
58     }
59 }

 参考网站

http://www.cnblogs.com/linjiqin/p/3535067.html

转载于:https://www.cnblogs.com/mao2080/p/7435341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值