使用CsvReader读取和写入csv文件的简单操作Demo:
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.Arrays;
/**
* Created by sherry on 2018/1/29.
* @noinspection ALL
*/
public class testJavaCsvAPI {
public static void main(String[] args) throws IOException {
String writerCsvFilePath = "src/resource/writer.csv";
CsvWriter csvWriter = new CsvWriter(writerCsvFilePath, ',', Charset.forName("UTF-8"));
String[] contents = {"Lily","五一","90","女"};
csvWriter.writeRecord(contents);
csvWriter.close();
String readerCsvFilePath = "src/resource/reader.csv";
CsvReader csvReader = new CsvReader(readerCsvFilePath, ',', Charset.forName("UTF-8"));
csvReader.readHeaders(); // 跳过表头 如果需要表头的话,不要写这句。
String[] head = csvReader.getHeaders(); //获取表头
while (csvReader.readRecord())
{
for (int i = 0; i < head.length; i++)
{
System.out.println(head[i] + ":" + csvReader.get(head[i]));
}
}
csvReader.close();
}
}
下面是我仿照csvWriter简化实现的CsvWriter
不论是csvWriter还是csvReader都提供用户自定义分隔符方法,使用方式是:
// 例如,要使用;作为csv的分隔符
csvReader.setDelimiter(';');
import java.io.*;
import java.nio.charset.Charset;
public class CsvWriterUtil {
private Writer outputStream;
private String fileName;
public char delimiter = ',';// 分隔符
public Charset charset;
private boolean initialized;
private boolean closed;
private boolean firstColumn;
public CsvWriterUtil(String fileName, char delimiter, Charset charset) {
this.closed = false;
this.initialized = false;
this.firstColumn = true;
if (fileName == null) {
throw new IllegalArgumentException("Parameter fileName can not be null.");
}
if (charset == null) {
throw new IllegalArgumentException("Parameter charset can not be null.");
}
this.fileName = fileName;
this.delimiter = delimiter;
this.charset = charset;
}
public CsvWriterUtil(String fileName) {
this(fileName, ',', Charset.forName("UTF-8"));
}
public CsvWriterUtil(String fileName, char delimiter) {
this(fileName, delimiter, Charset.forName("UTF-8"));
}
public char getDelimiter() {
return delimiter;
}
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}// 自定义分隔符
private void checkClosed() throws IOException {
if(this.closed) {
throw new IOException("This instance of the CsvWriter class has already been closed.");
}
}
private void checkInit() throws IOException {
if(!this.initialized) {
if(this.fileName != null) {
this.outputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.fileName), this.charset));
}
this.initialized = true;
}
}
public void write(String var1) throws IOException {
this.checkClosed();
this.checkInit();
if(var1 == null) {
var1 = "";
}
if(!this.firstColumn) {
this.outputStream.write(this.delimiter);
}
if(var1.length() > 0) {
var1 = var1.trim();
}
this.outputStream.write(var1);
this.firstColumn = false;
}
public void writeRecord(String[] var1) throws IOException {
if(var1 != null && var1.length > 0) {
for(int i = 0; i < var1.length; ++i) {
this.write(var1[i]);
}
this.endRecord();
}
}
public void endRecord() throws IOException {
this.checkClosed();
this.checkInit();
this.firstColumn = true;
}
public void flush() throws IOException {
this.outputStream.flush();
}
public void close() {
if(!this.closed) {
this.charset = null;
try {
if (this.initialized) {
this.outputStream.close();
}
} catch (Exception var3) {
;
}
this.outputStream = null;
this.closed = true;
}
}
}