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. wikioi1191 数轴染色

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着 我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后 剩余黑色点的个 ...

  2. mysql5.5 无法创建实例,error 16001

    今天想用jdbc做个小程序,结果发现好久不用的mysql不好用了,我装的是社区版(win7)环境下,按理说不可能出问题,找了一堆解决方案都没解决,准备重装的时候想把mysql服务停了,直接在dos输入 ...

  3. linux中的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的 ...

  4. hdu 5312 Sequence(数学推导+线性探查(两数相加版))

    Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...

  5. 导致flash屏幕重绘的几种方式及避免重绘的方法

    导致屏幕重绘的几种原因: 1.最常见的是情况就是舞台上的可视组件在形状.位置.状态(alpha, scale…)发生改变的时候会触发Flash Player 的重绘. 2.当一个DisplayObje ...

  6. xcode 执行时模拟器不可选的问题

    好久没写博客了,上一次是什么时候都想不起来了. 之前总认为脑袋记住了,用过了就能够了,干嘛要写博客,简直浪费时间.事实上没事写写博客优点还是挺多的.这样既能够对自己用过的和学到的东西做一个总结,也能提 ...

  7. Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)

    Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==== ...

  8. MVC Razor 一些常用的方法

    一.在ASP.NET MVC中,创建视图最典型的方式是调用一个action方法,它使用模型准备视图数据.action方法然后调用控制器的视图方法创建视图. 1 <% Html.RenderAct ...

  9. [转载]Linux 性能监控、测试、优化工具

    Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...

  10. (原)编译caffe时提示未定义的引用(undefined reference to)

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5864715.html 参考网址: https://github.com/BVLC/caffe/issu ...