csv文件其实就是格式化的txt文件,所以操作和txt文件差不多,直接上代码了
1、相关jar包代码
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.ostermiller</groupId>
<artifactId>utils</artifactId>
<version>1.07.00</version>
</dependency>
2、Csv文件写操作类代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import com.Ostermiller.util.CSVPrint;
import com.Ostermiller.util.CSVPrinter;
/**
*
* @author alex.wang
* csv文件写入
*
*/
public class CsvFilePrinter{
private CSVPrint csvPrint;
/**
*
* @param fileName 文件路径
* @param append 是否支持追加
* @throws IOException
*/
public CsvFilePrinter(String fileName,boolean append) throws IOException {
File file = new File(fileName);
if(!file.exists()){
csvPrint = new CSVPrinter(new FileWriter(fileName,append));
init();
}else{
csvPrint = new CSVPrinter(new FileWriter(fileName,append));
if(!append){
init();
}
}
}
public void init() throws IOException{
write(new String[]{"id","mac","val","date"});
}
public void write(String[] values) throws IOException {
csvPrint.writeln(values);
}
public static void main(String[] args) throws Exception {
String csvFile = "demo".concat("-").concat(DateFormatUtils.format(new Date(), "yyyyMMdd")).concat(".csv");
CsvFilePrinter print = new CsvFilePrinter(csvFile,false);
for(int i=0;i<10;i++){
print.write(new String[]{"50001"+i,"C914"+i,Integer.toString(-80+i),DateFormatUtils.format(new Date(), "yyyy-MM-dd")});
}
}
}
3、Csv文件解析类代码
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.Ostermiller.util.ExcelCSVParser;
import com.Ostermiller.util.LabeledCSVParser;
/**
*
* @author alex.wang
* csv文件解析器
*
*/
public class CsvFileParser{
private LabeledCSVParser csvParser;//csv解析器,对于第一行的表头信息,自动加载为索引关键字
private int currLineNum = -1;//文件所读到行数
private String[] currLine = null;//用来存放当前行的数据
/*
* 构造函数,
* Param: in InputStream 要解析的信息流
* throws IOException
*/
public CsvFileParser(InputStream in) throws IOException {
csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
currLineNum = csvParser.getLastLineNumber();
}
public CsvFileParser(String fileName) throws IOException {
InputStream in = new FileInputStream(fileName);
csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
currLineNum = csvParser.getLastLineNumber();
}
/*
* 检查是否还有数据
*
* return ture 还有一行数据,false 没有数据
*/
public boolean hasMore() throws IOException {
currLine = csvParser.getLine();
currLineNum = csvParser.getLastLineNumber();
if (null == currLine)
return false;
return true;
}
/*
* 返回当前行数据,关键字所指向的数据
* param:String filedName 该行的表头
* return:String 返回当前行数据,关键字所指向的数据
*/
public String getByFieldName(String fieldName) {
return csvParser.getValueByLabel(fieldName);
}
/*
* 关闭解析器
*
*
*/
public void close() throws IOException {
csvParser.close();
}
/*
* 读取当前行数据
*
* return String[] 读取当前行数据
*/
public String[] readLine() throws IOException {
currLine = csvParser.getLine();
currLineNum = csvParser.getLastLineNumber();
return currLine;
}
public int getCurrLineNum(){
return currLineNum;
}
public static void main(String[] args) throws Exception {
//创建解析信息流
InputStream in=new FileInputStream(new File("demo.csv"));
//实例解析器CsvFileParser
CsvFileParser parser=new CsvFileParser(in);
//读取数据
while(parser.hasMore()){
System.out.print(parser.getByFieldName("time")+" ");//time 系表头数据
System.out.print(parser.getByFieldName("total")+" ");
}
parser.close();
}
}