通过 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 ...
随机推荐
- 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new
引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...
- 传智播客C/C++学员荣膺微软&Cocos 2d-x黑客松最佳创新奖
6月30日,历时32小时的微软开放技术Cocos 2d-x 编程黑客松在北京望京微软大厦成功落下帷幕,这是微软开放技术首次联合Cocos 2d-x 在中国举办黑客松.此次活动共有包括传智播客C/ ...
- 二叉查找树的Insert和Delete操作
struct TreeNode{ SearchTree Left; SearchTree Right; ElementType Ele; }; /*递归一定有出口*/ /*递归代码就是要重复使用*/ ...
- JenKins 环境搭建 for Centos6.5
1,JenKines简单介绍--图解
- 图片缩放JavaScript原生实现
function scalImg(aLi){ for(var i=0,l=aLi.length;i<l;i++){ var oImg = new Image(), oLi = aLi[i], i ...
- POJ 2029 DP || 暴力
在大矩形中找一个小矩形 使小矩形包括的*最多 暴力或者DP 水题 暴力: #include "stdio.h" #include "string.h" int ...
- 【转】HtmlAgilityPack 之 HtmlNode类
[转]HtmlAgilityPack 之 HtmlNode类 HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 ...
- Linux下Oracle常见安装错误[Z]
#./runInstaller之后出现如下的错误信息: RedHat AS5 x86上安装Oracle1020 Exception in thread "main" java.la ...
- iOS 打开系统设置
NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"]; [[UIApplication sharedApplication] ope ...
- C#用网易邮箱发送邮件(同步异步)
SmtpClient smtpServer = new SmtpClient("smtp.163.com"); smtpServer.Port = ; smtpServer.Cre ...