import com.univocity.parsers.csv.CsvFormat;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;

创建csv文件:

public static void createCSVFile(String[] heads, List<Object[]> rows, String outPutPath, String filename)
    {

// CsvWriter (and all other file writers) work with an instance of
        // java.io.Writer
        File csvFile = new File(outPutPath + filename + ".csv");
        File parent = csvFile.getParentFile();
        if (parent != null && !parent.exists())
        {
            parent.mkdirs();
        }
        try
        {
            csvFile.createNewFile();
            Writer fileWriter = new FileWriter(csvFile);

// By default, only values that contain a field separator are enclosed within quotes.
            // If quotes are part of the value, they are escaped automatically as well. Empty rows are discarded automatically.
            // Set the field delimiter to ';', the default value is ','
            CsvWriterSettings settings = new CsvWriterSettings();
            CsvFormat format = settings.getFormat();
            format.setDelimiter(';');

CsvWriter writer = new CsvWriter(fileWriter, settings);

// Write the record headers of this file
            writer.writeHeaders(heads);

// Write contents and close the given output Writer instance.
            writer.writeRowsAndClose(rows);
        } catch (Exception e)
        {
            e.printStackTrace();
            logger.error(e);
        }
    }

读取csv文件:

public static List<MyType> ReadCSV(String filePath) throws IOException {
        List<MyType> eslImports = new ArrayList<MyType>();

File file = new File(filePath);
        InputStream in = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(in, "UTF-8");

CsvParserSettings settings = new CsvParserSettings();
        settings.getFormat().setLineSeparator("\n");

CsvParser parser = new CsvParser(settings);
        //读取数据到列表
        List<String[]> allRows = parser.parseAll(reader);

//处理读取到的数据

.....

}

使用univocity-parsers创建和读取csv文件的更多相关文章

  1. python 使用read_csv读取 CSV 文件时报错

    读取csv文件时报错 df = pd.read_csv('c:/Users/NUC/Desktop/成绩.csv' ) Traceback (most recent call last):  File ...

  2. 自动化测试框架selenium+java+TestNG——读取csv文件

    读取csv文件可以直接读取,也可以使用javacsv.jar,后者比较简单,这个也可以变相认为是对表格的处理,我们可以在表格中做好数据,存储成csv格式的文件,后续对xlsx表格的操作抽个时间再记录下 ...

  3. C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据

    1. CSV-百度百科 2. 代码 #pragma once //Microsoft Visual Studio 2015 Enterprise #include<iostream> #i ...

  4. 使用pandas读取csv文件和写入文件

    这是我的CSV文件 读取其中得tempo这一列 import pandas as pd #导入pandas包 data = pd.read_csv("E:\\毕设\\情感识别\\Music- ...

  5. sparkR读取csv文件

    sparkR读取csv文件 The general method for creating SparkDataFrames from data sources is read.df. This met ...

  6. VB6.0 读取CSV文件

    最近做了一个Upload文件的需求,文件的格式为CSV,读取文件的方法整理了一下,如下: 1.先写了一个读取CSV文件的Function: '读取CSV文件 '假设传入的参数strFile=C:\Do ...

  7. php读取csv文件,在linux上出现中文读取不到的情况 解决方法

    今,php读取csv文件,在linux上出现中文读取不到的情况,google,后找到解决办法<?phpsetlocale(LC_ALL, 'zh_CN');$row = 1;$handle = ...

  8. Python 读取csv文件到excel

    朋友问我如何通过python把csv格式的文件另存为xls文件,自己想了想通过读取csv文件然后再保存到xls文件中即可,也许还有其他简单的方法,但这里也为了练习python语法及其他知识,所以采用了 ...

  9. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法

    1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...

随机推荐

  1. Java迭代器深入理解及使用

    Iterator(迭代器) 作为一种设计模式,迭代器可以用于遍历一个对象,对于这个对象的底层结构开发人员不必去了解. java中的Iterator一般称为“轻量级”对象,创建它的代价是比较小的.这里笔 ...

  2. 程序员求职之道(《程序员面试笔试宝典》)之程序设计基础(static的使用)?

    在C语言中,关键字static的意思是静态,它有三个明显的作用:首先,在函数体内,静态变量具有"记忆"功能,即一个被声明为静态的变量在这一函数被调用过程中其值维持不变.其次,在模块 ...

  3. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  4. Ajax_post发送

    $('#img_file_del_3').click(function() { var data={name:$('#img_file_del_3').attr('name')}; var url=' ...

  5. C#Http编程

    c# 模拟 网页实现12306登陆.自动刷票.自动抢票完全篇(转) 这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童 ...

  6. UILabel+Create

    #import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...

  7. linux下修改防火墙端口对外开放方法

    ---linix CentOS7的防火墙换成了firewall了,这里做一些记录,下面是一些命令:添加例外端口:# firewall-cmd --add-port=8080/tcp删除例外端口:# f ...

  8. 页面动态数据的滚动效果——jquery滚动组件(vticker.js)

    <script language="javascript" src="lirms/Test/jquery-1.4.2.js"></script ...

  9. [置顶] 【cocos2d-x入门实战】微信飞机大战之十二:分数的本地存储

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/12036237 作为一个单机游戏,连分数存储的的功能都没有,让它怎么在单机游戏圈里混 ...

  10. [Protractor] Protractor Interactive with elementor

    Install: npm install -g elementor Then run: webdriver-manager start Lets say if we want to test 'htt ...