使用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 ...
随机推荐
- 多个文件目录下Makefile的写法
1.前言 目前从事于linux下程序开发,涉及到多个文件,多个目录,这时候编译文件的任务量比较大,需要写Makefile.关于Makefile的详细内容可以参考网上流传非常广泛的<跟我一起写Ma ...
- hdu3095-Eleven puzzle(双向搜索+哈希)
Partychen invents a new game named “Eleven Puzzle” .Just like the classic game “Eight Puzzle”,but th ...
- poj 2411 Mondriaan's Dream_状态压缩dp
题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- discuz!NT 常用操作
一.编辑模版,需在后台模版管理里编辑并提交,这样系统能批量更改相关模版.如:更改登录. 二.config.a 表明:config表示配置文件,a表示配置节名称为a,需在路径 upload_files\ ...
- pyqt listview基础学习01
from decimal import * from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * im ...
- pyqt 托盘例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...
- IOS 采用https 协议访问接口
申请好证书后,发现ios 仍无法使用https协议访问到数据,发现ios 需要ssl 支持 TLS1.2 . 更改nginx 配置: ssl_protocols TLSv1 TLSv1. TLSv1. ...
- nginx 搭建rtmp流媒体所用资源
Linux: 1.nginx安装包,自行下载. 2.nginx_mod_h264_streaming-2.2.7下载 3.nginx-rtmp-module-master下载 Windows: ngi ...
- 创建对象时引用的关键字,assign,copy,retain
创建对象时引用的关键字:assign: 简单赋值,不更改索引计数(强引用)copy: 建立一个索引计数为1的对象,然后释放旧对象retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索 ...