本帖最后由 好彩哥 于 2013-9-8 22:08 编辑
CSV是Comma Separated Values(逗号分隔值)的英文缩写,通常都是纯文本文件。首先先来了解csv文件的基本格式:
年 制造商 型号 说明 价值 <学习分享更美好,学云的有木有有啊!!!!>
1997 Ford E350 ac, abs, moon 3000.00
1999 Chevy Venture "Extended Edition" ? 4900.00
1999 Chevy Venture "Extended Edition, Very Large" ? 5000.00
1996 Jeep Grand Cherokee MUST SELL!
air, moon roof, loaded 4799.00
上面表格内容若以CSV格式表示就会像下列:
年,制造商,型号,说明,价值
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!air, moon roof, loaded",4799.00
java如何操作读写csv呢?可以使用opencsv、javacsv等,本文主要介绍javacsv操作csv文件。
读csv代码如下:
[html]
import com.csvreader.CsvReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author yh
*/
public class CsvUtil {
//读取csv文件
public List<String[]> readCsv(String filePath) throws Exception {
List<String[]> csvList = new ArrayList<String[]>();
if (isCsv(filePath)) {
CsvReader reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
reader.readHeaders(); // 跳过表头 如果需要表头的话,不要写这句。
while (reader.readRecord()) { //逐行读入除表头的数据
csvList.add(reader.getValues());
}
reader.close();
} else {
System.out.println("此文件不是CSV文件!");
}
return csvList;
}
//判断是否是csv文件
private boolean isCsv(String fileName) {
return fileName.matches("^.+\\.(?i)(csv)$");
}
//方法测试
public static void main(String[] args) throws Exception {
String filepath = "C:/群组.csv";
CsvUtil su = new CsvUtil();
List<String[]> list = su.readCsv(filepath);
for (int r = 0; r < list.size(); r++) {
for (int c = 0; c < list.get(r).length; c++) {
String cell = list.get(r)[c];
System.out.print(cell + "\t");
}
System.out.print("\n");
}
}
}
import com.csvreader.CsvReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author yh
*/
public class CsvUtil {
//读取csv文件
public List<String[]> readCsv(String filePath) throws Exception {
List<String[]> csvList = new ArrayList<String[]>();
if (isCsv(filePath)) {
CsvReader reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
reader.readHeaders(); // 跳过表头 如果需要表头的话,不要写这句。
while (reader.readRecord()) { //逐行读入除表头的数据
csvList.add(reader.getValues());
}
reader.close();
} else {
System.out.println("此文件不是CSV文件!");
}
return csvList;
}
//判断是否是csv文件
private boolean isCsv(String fileName) {
return fileName.matches("^.+\\.(?i)(csv)$");
}
//方法测试
public static void main(String[] args) throws Exception {
String filepath = "C:/群组.csv";
CsvUtil su = new CsvUtil();
List<String[]> list = su.readCsv(filepath);
for (int r = 0; r < list.size(); r++) {
for (int c = 0; c < list.get(r).length; c++) {
String cell = list.get(r)[c];
System.out.print(cell + "\t");
}
System.out.print("\n");
}
}
}
写csv代码如下:
[html]
/**
* 写入CSV文件
*/
public static void WriteCsv(){
try {
String csvFilePath = "C:/群组.csv";
CsvWriter wr =new CsvWriter(csvFilePath,',',Charset.forName("SJIS"));//日文编码
String[] contents = {"警告信息","非法操作","没有权限","操作失败"};
wr.writeRecord(contents);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 写入CSV文件
*/
public static void WriteCsv(){
try {
String csvFilePath = "C:/群组.csv";
CsvWriter wr =new CsvWriter(csvFilePath,',',Charset.forName("SJIS"));//日文编码
String[] contents = {"警告信息","非法操作","没有权限","操作失败"};
wr.writeRecord(contents);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
|
|