【转】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("
");
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