通过 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 ...
随机推荐
- 【转】Logistic regression (逻辑回归) 概述
Logistic regression (逻辑回归)是当前业界比较常用的机器学习方法,用于估计某种事物的可能性.比如某用户购买某商品的可能性,某病人患有某种疾病的可能性,以及某广告被用户点击的可能性等 ...
- Java WebService把Date类型转换成XMLGregorianCalendar
JavaEE 的WebService中的Date类型在Web应用中调set方法的时候,默认情况下,JAXB将xsd:date, xsd:time, 和xsd:dateTime映射为XMLGregori ...
- 饿了么移动APP的架构演进(转)
原文:http://www.jianshu.com/p/2141fb0dc62c 文/圣迪(简书作者)原文链接:http://www.jianshu.com/p/2141fb0dc62c著作权归作者所 ...
- Dreamweaver PHP代码护眼配色方案
结果展示 [1]主菜单选择编辑->首选项.在分类中选择"字体".设置代码视图的字体为Courier New [2]在分类中选择 "代码颜色",点击 &qu ...
- Docker简单介绍
Docker简单介绍 Docker是一个能够把开发的应用程序非常方便地部署到容器的开源引擎.由Docker公司团队编写,基于Apache 2.0开源授权协议发行.Docker的主要目的例如以下: 提供 ...
- OCP prepare 20140628
1. null if nvl nvl2 NULLIF函数 Oracle NULLIF函数语法为NULLIF(表达式1,表达式2),如果表达式1和表达式2相等则返回空值,如果表达式1 ...
- 经典:十步完全理解 SQL
经典:十步完全理解 SQL 来源:伯乐在线 链接:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完 ...
- Mysql中时间的操作笔记
1.创建修改表时,为datetime字段设置当前时间为默认值 CREATE TABLE `NewTable` ( `id` int(11) NOT NULL AUTO_INCREMENT , `des ...
- 安装Eclipse Color Theme
我们都知道eclipse默认的颜色主题是白色的背景,但是如果想改变代码编辑区的背景颜色,需要怎么办呢? 今天给大家介绍一个非常赞的eclipse,可以很方便的根据自己的需求选择喜欢的颜色主题,其他的不 ...
- objective -c こだわり
You make an object by creating an instance of a particular class. You do this by allocating the obje ...