为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

java读取csv

2017-11-22 4页 doc 16KB 20阅读

用户头像

is_180829

暂无简介

举报
java读取csvjava读取csv import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; Mc1081 import org.apache.log4j.Logger; import com.csvreader.CsvReader; import...
java读取csv
java读取csv import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; Mc1081 import org.apache.log4j.Logger; import com.csvreader.CsvReader; import com.csvreader.CsvWriter; /** * CSV?âÊÔÀà * */ public class CSVTest { protected static Logger logger = Logger.getLogger(CSVTest.class); /** * ?ÁÈ?CSVÎÄ?þ * @param file * @return */ public static List readCsv(String file) { List list = new ArrayList(); CsvReader reader = null; try { // ?õÊ???CsvReader??Ö???ÁÐ?Ö?ô?ûºÍ×Ö?û?àÂë reader = new CsvReader(file, ',', Charset.forName("GBK")); while (reader.readRecord()) { // ?ÁÈ?Ã?ÐÐÊý?ÝÒÔÊý×éÐÎÊ??µ?Ø String[] str = reader.getValues(); if (str != null && str.length > 0) { if (str[0] != null && !"".equals(str[0].trim())) { System.out.println(str.length); System.out.println(str[1]); list.add(str); } } } } catch (FileNotFoundException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (reader != null) { reader.close(); } } return list; } /** * Ð?ÈëCSVÎÄ?þ */ public static void writeCsv(List content, String path) { CsvWriter writer = null; try { writer = new CsvWriter(path, ',', Charset.forName("GBK")); if(content != null && content.size() != 0) { for(String[] strArray : content) { writer.writeRecord(strArray); } } else { // ×öÒì????Àí } } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if(writer != null) { writer.close(); } } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String readPath = "F:" + File.separator + "test" + File.separator + "muti-test.csv"; String writePath = "F:" + File.separator + "test" + File.separator + "muti-test-output.csv"; List result = readCsv(readPath); for(String[] strArray : result) { for(String s : strArray) { System.out.print(s + ","); } System.out.println(); } writeCsv(result, writePath); Thread.sleep(999999); } }
/
本文档为【java读取csv】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索