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);
}
}
}

  

最新文章

  1. Yii Model中添加默认搜索条件
  2. zedboard如何从PL端控制DDR读写(七)
  3. 传递消息--第三方开源--EventBus的简单使用
  4. object_c函数多个返回值
  5. SVG ViewBox
  6. 图形编程(数值微分DDA)
  7. CSS小记(持续更新......)
  8. 走进React
  9. Memcached和Redis简介
  10. dnsmasq一次成功的配置
  11. [Shoi2007]Vote 善意的投票
  12. 关于embed的一些使用兼容
  13. android TextView 垂直自动滚动字幕实现
  14. LeetCode(32)-Binary Tree Level Order Traversal
  15. python装饰器 练习
  16. zooland 新开源的RPC项目,希望大家在开发的微服务的时候多一种选择,让微服务开发简单,并且容易上手。
  17. 1. myeclipse设置jsp默认打开方式为jsp Editor
  18. Vscode 修改为中文语言
  19. 电脑提示‘您需要来自Administration的权限才能对此文件夹进行更改’怎么删除文件
  20. URAL 1997 Those are not the droids you&#39;re looking for

热门文章

  1. json、map互转
  2. Redis短结构与分片
  3. java于23设计模式
  4. android_Intent对象初步(Activity传统的价值观念)
  5. Ubuntu系统安装stardict(星际译王)词典
  6. Moss 几个编程技巧
  7. javascript高级知识分析——定义函数
  8. bootstrap注意事项(五)表单
  9. datatable赋值行
  10. Java语言导学笔记 Chapter 9 IO