/**
* 读取CSV文件
*
* @param file
* @return
*/
public static ArrayList<Map<String, String>> readCsv(String file)
{
logger.info("file:" + file);
ArrayList<String[]> list = new ArrayList<String[]>();
ArrayList<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
CsvReader reader = null;
try
{
// 初始化CsvReader并指定列分隔符和字符编码
reader = new CsvReader(file, ',', Charset.forName("GBK"));
while (reader.readRecord())
{
// 读取每行数据以数组形式返回
String[] str = reader.getValues();
if (str != null && str.length > 0)
{
if (!str.toString().trim().equals(""))
{
list.add(str);
}
}
}
if (list.size() != 0 && list.size() != 1)
{
for (int i = 1; i < list.size(); i++)
{
map = new HashMap<String, String>();
for (int j = 0; j < list.get(0).length; j++)
{
if (j >= list.get(i).length)
{
break;
}
map.put(list.get(0)[j], list.get(i)[j]);
}
listMap.add(map);
}
}
}
catch (FileNotFoundException e)
{
}
catch (IOException e)
{
}
finally
{
if (reader != null)
{
reader.close();
}
}
return listMap;
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
Mc1081
import org.apache.log4j.Logger;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
/**
* CSV测试类
*
*/
public class CSVTest {
protected static Logger logger = Logger.getLogger(CSVTest.class);
/**
* 读取CSV文件
* @param file
* @return
*/
public static List<String[]> readCsv(String file) {
List<String[]> list = new ArrayList<String[]>();
CsvReader reader = null;
try {
// 初始化CsvReader并指定列分隔符和字符编码
reader = new CsvReader(file, ',', Charset.forName("GBK"));
while (reader.readRecord()) {
// 读取每行数据以数组形式返回
String[] str = reader.getValues();
if (str != null && str.length > 0) {
if (str[0] != null && !"".equals(str[0].trim())) {
System.out.println(str.length);
System.out.println(str[1]);
list.add(str);
}
}
}
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (reader != null) {
reader.close();
}
}
return list;
}
/**
* 写入CSV文件
*/
public static void writeCsv(List<String[]> content, String path) {
CsvWriter writer = null;
try {
writer = new CsvWriter(path, ',', Charset.forName("GBK"));
if(content != null && content.size() != 0) {
for(String[] strArray : content) {
writer.writeRecord(strArray);
}
} else {
// 做异常处理
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if(writer != null) {
writer.close();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String readPath = "F:" + File.separator + "test" + File.separator + "muti-test.csv";
String writePath = "F:" + File.separator + "test" + File.separator + "muti-test-output.csv";
List<String[]> result = readCsv(readPath);
for(String[] strArray : result) {
for(String s : strArray) {
System.out.print(s + ",");
}
System.out.println();
}
writeCsv(result, writePath);
Thread.sleep(999999);
}
}
微信关注我们
原文链接:https://yq.aliyun.com/articles/644812
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
相关文章
发表评论
资源下载
更多资源Mario,低调大师唯一一个Java游戏作品
马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。
Apache Tomcat7、8、9(Java Web服务器)
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Java Development Kit(Java开发工具)
JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。
Sublime Text 一个代码编辑器
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。