通过 CsvListWriter 读写.csv文件辅助类
package cn.gov.cnis.db; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.supercsv.io.CsvListReader;
import org.supercsv.io.CsvListWriter;
import org.supercsv.prefs.CsvPreference; public class OperateCsv { /**
* 读取csv文件(不带头部)
*
* @param String filePath
* @return csv文件组装成list
* @throws IOException
*/
static public List<String[]> getContentFromFile(String filePath) throws IOException {
File file = new File(filePath);
List<String[]> content = new ArrayList<String[]>();
CsvListReader reader = new CsvListReader(new FileReader (file), CsvPreference.EXCEL_PREFERENCE);
reader.getCSVHeader(true);// 去除头部字段声明
List<String> line = new ArrayList<String>();
while ((line = reader.read()) != null) {
content.add(line.toArray(new String[] {}));
}
return content;
} /**
* 读取csv文件(带头部)
*
* @param String filePath
* @return csv文件组装成list
* @throws IOException
*/
static public List<String[]> getDetailFromFile(String filePath) throws IOException {
File file = new File(filePath);
List<String[]> content = new ArrayList<String[]>();
CsvListReader reader = new CsvListReader(new FileReader(file), CsvPreference.EXCEL_PREFERENCE);
String[] header = reader.getCSVHeader(true);
content.add(header);
List<String> line = new ArrayList<String>();
while ((line = reader.read()) != null) {
content.add(line.toArray(new String[] {}));
}
return content;
} /**
* 读取csv文件的头部
*
* @param String filePath
* @return csv文件的头部
* @throws IOException
*/
static public String[] getHeaderFromFile(String filePath) throws IOException {
File file = new File(filePath);
CsvListReader reader = new CsvListReader(new FileReader (file), CsvPreference.EXCEL_PREFERENCE);
return reader.getCSVHeader(true);
} /**
* 写入csv文件
*
* @param String filePath
* @param header
* 头部
* @param content
* 内容
* @throws IOException
*/
static public void writeToCsv(String filePath, String[] header, List<String[]> content)
throws IOException {
File file = new File(filePath);
CsvListWriter writer = new CsvListWriter(new FileWriter(file), CsvPreference.EXCEL_PREFERENCE);
writer.writeHeader(header);
for (String[] str : content) {
writer.write(str);
}
writer.close();
} /**
* 写入csv文件
*
* @param String filePath
* @param content
* 内容
* @throws IOException
*/
static public void writeContentToCsv(String filePath, List<String[]> content)
throws IOException {
File file = new File(filePath);
CsvListWriter writer = new CsvListWriter(new FileWriter(file),CsvPreference.EXCEL_PREFERENCE);
for (String[] str : content) {
writer.write(str);
}
writer.close();
} /**
* 写入csv文件(头部)
*
* @param String filePath
* @param content
* 内容
* @throws IOException
*/
static public void writeHeaderToCsv(String filePath, String[] header) throws IOException {
File file = new File(filePath);
CsvListWriter writer = new CsvListWriter(new FileWriter(file),CsvPreference.EXCEL_PREFERENCE);
writer.writeHeader(header);
writer.close();
} public static void main(String[] args) throws IOException {
OperateCsv operateCsv = new OperateCsv();
String file = "e:/Projects/Java/luke-3.4.0_1/AVDDOCS/export/73602.csv";
List<String[]> content = operateCsv.getDetailFromFile(file);
String[] header = operateCsv.getHeaderFromFile(file);
for (String[] str : content) {
for (int i = 0; i < str.length; i++) { System.out.println(str[i] + " " + str[i + 1] + " "
+ str[i + 2] + " " + str[i + 3]);
i = i + 4; // System.out.println(str[i]); }
String file1 = "D:/2.csv";
operateCsv.writeToCsv(file1, header, content);
operateCsv.writeHeaderToCsv(file1, header);
operateCsv.writeContentToCsv(file1, content);
}
}
}
通过 CsvListWriter 读写.csv文件辅助类的更多相关文章
- 用opencsv文件读写CSV文件
首先明白csv文件长啥样儿: 用excel打开就变成表格了,看不到细节 推荐用其它简单粗暴一点儿的编辑器,比如Notepad++, csv文件内容如下: csv文件默认用逗号分隔各列. 有了基础的了解 ...
- 使用Python读写csv文件的三种方法
Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...
- python3读写csv文件
python读取CSV文件 python中有一个读写csv文件的包,直接import csv即可.利用这个python包可以很方便对csv文件进行操作,一些简单的用法如下. 1. 读文件 csv_ ...
- python读写csv文件
文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...
- 利用JavaCSV API来读写csv文件
http://blog.csdn.net/loongshawn/article/details/53423121 http://javacsv.sourceforge.net/ 转载请注明来源-作者@ ...
- 使用 Apache Commons CSV 读写 CSV 文件
有时候,我们需要读写 CSV 文件,在这里给大家分享Apache Commons CSV,读写 CSV 文件非常方便. 具体官方文档请访问Apache Commons CSV. 官方文档已经写得很详细 ...
- python3使用csv包,读写csv文件
python操作csv,现在很多都用pandas包了,不过python还是有一个原始的包可以直接操作csv,或者excel的,下面举个例子说明csv读写csv文件的方法: import os impo ...
- C/C++读写csv文件
博客转载自:http://blog.csdn.net/u012234115/article/details/64465398 C++ 读写CSV文件,注意一下格式即可 #include <ios ...
- JAVA读写CSV文件
最近工作需要,需要读写CSV文件的数据,简单封装了一下 依赖读写CSV文件只需引用`javacsv`这个依赖就可以了 <dependency> <groupId>net.sou ...
随机推荐
- API 设计: RAML、Swagger、Blueprint三者的比较
API设计工具中常常会拿RAML.Swagger.Blueprint这三种工具进行讨论比较,它们都是用来描述和辅助API开发的,只是它们之间的侧重有所不同. RAML RAML(RESTful API ...
- Final Exam Arrangement(ZOJ)
In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own t ...
- 服务端API的OAuth认证实现
http://stackoverflow.com/questions/12499602/body-joints-angle-using-kinect?rq=1 新浪微博跟update相关的api已经挂 ...
- iOS开发中视图相关的小笔记:push、modal、popover、replace、custom
在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的. 在iphone中,segue有:push,modal,和custom三种不同的类型, ...
- 获取一个请求的URL内容
using System.Net; 1. // 创建一个请求的URL. WebRequest request = WebRequest.Create("http://www ...
- VS2010在C盘下生成的.iTrace文件解决办法 ,c盘偷偷的减少,心很烦啊,找了半天才知道是这个问题
用Visual Studio 2010后发现我的c盘变得越来越小了,刚开始通过优化工具清理c盘,但是无论怎么做都不能将c的内存有效提升,之后一个一个目录的查找之后才知道有个文件夹C:\ProgramD ...
- JavaScript之JSON
一.简介:Json是JavaScript中读取结构化数据更好的方式.因为Json数据可以直接传给eval(),而且不必创建DOM对象.Json是一种数据格式,不是一种编程语言,虽然具有相同的语法形式, ...
- 如何用SQL操作数据------告别标题党
额,首先跟大家道一个歉,由于本人上次利用标题来骗访问,对各位大哥大姐,叔叔阿姨,弟弟妹妹,and舅子老表的时间及流量造成了严重的浪费,本人深表歉意(好吧,其实本人内心还是有那么一丢丢的自豪的,毕竟是一 ...
- 有关MyISAM引擎的锁定机制
本文介绍下,mysql数据库中MyISAM引擎的锁定机制的相关知识,感兴趣的朋友可以参考下. 本节内容: MyISAM引擎的锁定机制 在mysql数据库中,MyISAM存储引擎适合于读频率远大于写频率 ...
- js函数定时器,定时读取系统实时连接数
function GetDeviceInfo() { setInterval(function() { GetDeviceRealtimeConnect(); ...