【轉】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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM