通过 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 ...
随机推荐
- 【自学php】第一天-macbook上配置php
今天MacBook到手了,就正式开始学习php了.先搭个环境,由于MacBook自带了Apache和php所以只要修改下配置启动就可以了. 1.启用root用户(如果不启用root,下面的命令前都要加 ...
- leftpad填充函数;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL存储过程的基本函数
(1).字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回subst ...
- 关于优化性能<主要是速度方面>的个人心得 【转】
一个web项目后期的维护主要在于性能方面.数据吞吐量一旦增大各种bug都出来了.那些通过硬件<数据库分表,数据库主从分离,读写分离>等的一些手段此处就不多说了.本文主要在编码方面做一个性能 ...
- 我终于解决UM编辑器了 泪......
气死我了..... 好不容易测试好了....更显得我笨了..... 原来....什么都不用改 只改了2个小位置....真的是.....回首自己 不敢看 0.0 OK 记下步骤 以免以后忘记 将 ...
- 查询SQL中某表里有多少列包含某字段
select c.name from SYSCOLUMNS as c left join SYSOBJECTS as t on c.id=t.id where c.name like '这里是某个字段 ...
- ruby中输入命令行编译sass(ruby小白)
Ruby(或cmd中)输入命令行编译sass步骤如下: (1)举例而言:首先在F盘下建立一个总文件夹,比如test文件夹:其次在该文件夹下建立html,images,js,sass等文件夹. (2)在 ...
- memcached在注册表的位置
HKEY_LOCAL_MACHINE-System-ControlSet001-services-Memcached Server
- UITextField总结--博主总结的真好
忍不住copy过来http://www.cnblogs.com/wengzilin/archive/2012/03/13/2393985.html 还有些不错的博客推荐给大家http://blog.c ...
- C# List<T>中Select List Distinct()去重复
List<ModelJD> data = myDalJD.GetAllDataList(); List<string> list= new List<string> ...