使用univocity-parsers创建和读取csv文件
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文件的更多相关文章
- python 使用read_csv读取 CSV 文件时报错
读取csv文件时报错 df = pd.read_csv('c:/Users/NUC/Desktop/成绩.csv' ) Traceback (most recent call last): File ...
- 自动化测试框架selenium+java+TestNG——读取csv文件
读取csv文件可以直接读取,也可以使用javacsv.jar,后者比较简单,这个也可以变相认为是对表格的处理,我们可以在表格中做好数据,存储成csv格式的文件,后续对xlsx表格的操作抽个时间再记录下 ...
- C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据
1. CSV-百度百科 2. 代码 #pragma once //Microsoft Visual Studio 2015 Enterprise #include<iostream> #i ...
- 使用pandas读取csv文件和写入文件
这是我的CSV文件 读取其中得tempo这一列 import pandas as pd #导入pandas包 data = pd.read_csv("E:\\毕设\\情感识别\\Music- ...
- sparkR读取csv文件
sparkR读取csv文件 The general method for creating SparkDataFrames from data sources is read.df. This met ...
- VB6.0 读取CSV文件
最近做了一个Upload文件的需求,文件的格式为CSV,读取文件的方法整理了一下,如下: 1.先写了一个读取CSV文件的Function: '读取CSV文件 '假设传入的参数strFile=C:\Do ...
- php读取csv文件,在linux上出现中文读取不到的情况 解决方法
今,php读取csv文件,在linux上出现中文读取不到的情况,google,后找到解决办法<?phpsetlocale(LC_ALL, 'zh_CN');$row = 1;$handle = ...
- Python 读取csv文件到excel
朋友问我如何通过python把csv格式的文件另存为xls文件,自己想了想通过读取csv文件然后再保存到xls文件中即可,也许还有其他简单的方法,但这里也为了练习python语法及其他知识,所以采用了 ...
- 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...
随机推荐
- HTTP常见的状态码
状态码的职责是当客户端向服务器端发送请求时,描述返回请求结果.借助状态码,用户可以知道服务器端是正常处理了请求,还是出现了什么错误.RFC2616定义的状态码,由3位数字和原因短信组成.数字中的第一位 ...
- A站有一个页面需要PV统计 A站读写该数据 B站读该数据 需要数据同步
A站弄个缓存,并且开放出一个读取借口给B站 B站读取数据的时候,调用该接口和数据库内的数据累加,然后进行限时即可 ---------------------- 另外其他方法 session服务.mem ...
- 精通find命令
一.前言 find命令是linux使用过程中经常用到的命令,但可能大家只会如下使用find find ./ 或者这样使用 find ./ | grep str 上述命令等同于 find ./ -nam ...
- 【HDU2120】Ice_cream's world I(并查集基础题)
查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...
- WifiDog系统
WifiDog:A captive portal suite What is it composed of ? A: It is composed of 2 components: The clien ...
- PHP初识
1. php是一种跨平台的语言,支持几乎全部的数据库. 以前觉得PHP与MYSQL是黄金组合,对于PHP能否支持MSSQL没有过了解,PHP支持几乎全部的数据库,也支持MSSQL(5.2.X版本可以用 ...
- 把分类的select写在moden里做成一个组件 高洛峰
function selectform($name="pid", $pid=0) { $data = $this->field('id, concat(path, " ...
- [R语言画图]气泡图symbols
绘制气泡图主要使用函数symbols(x,y,circle=r).当中x.y是坐标轴,r是每一个点的半径. x<-rnorm(10) y<-rnorm(10) r<-abs(rnor ...
- 修改tomcat访问路径
<Context path="/pc" docBase="/data/www/8084/kabao-pc-consume/" reloadable=&qu ...
- ARM9嵌入式学习笔记(1)-Linux命令
ARM9嵌入式学习笔记(1)-Linux命令 实验1-1-2 Linux常见命令使用 添加用户useradd smb; 设置账户密码passwd smb; 切换用户su - root 关机命令shut ...