java解析CSV文件
2012-02-16 16:58
435 查看
package util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
private File cvsFile;
public CSVReader() {
}
public CSVReader(File cvsFile, File logFile) {
this.cvsFile = cvsFile;
}
public File getCvsFile() {
return cvsFile;
}
public void setcvsFile(File cvsFile) {
this.cvsFile = cvsFile;
}
public void load(Loader loader) {
try {
// 读取CVS文件
BufferedReader reader = new BufferedReader(new FileReader(this.cvsFile));
// 逐行读取,并导入数据到数据库
String line = null;
// 行号
int rownum = 1;
while ((line = reader.readLine()) != null) {
try {
loader.load(parseLine(line));
} catch(Exception e) {
}
// 行号加一
rownum ++;
}
} catch (FileNotFoundException e) {} catch (IOException e) {}
}
private static List<String> parseLine(String line) {
// 解析字符串
List<String> tokenList = new ArrayList<String>();
StringBuffer token = new StringBuffer();
// 是否在引号内
boolean inQuotation = false;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == ',' && !inQuotation) {
tokenList.add(token.toString());
token.setLength(0);
} else if (c == '"') {
if (inQuotation) {
char d = line.charAt(i+1);
if (d == '"') {
token.append(c);
i ++;
} else {
inQuotation = !inQuotation;
}
} else {
inQuotation = !inQuotation;
}
} else {
token.append(c);
}
}
tokenList.add(token.toString());
// 返回解析结果
return tokenList;
}
public static void main(String[] args) throws Exception {
CSVReader.readCSV("D:/TestFile1.csv");
File cvsFile = new File("D:/TestFile1.csv");
File logFile = new File("D:/Load.log");
CSVReader loader = new CSVReader(cvsFile, logFile);
loader.load(new Loader(){
public void load(List recFieldList) throws Exception{
for (int i = 0; i < recFieldList.size(); i++) {
System.out.println("F[" + i + "]=" + recFieldList.get(i).toString().replaceAll("'", ""));
}
}
});
}
public static List<String[]> readCSV(String filepath) throws IOException {
//filepath ="D:/TestFile1.csv"
File file = new File(filepath);//到目录下取文件
BufferedReader br = null;
List<String[]> list = new ArrayList<String[]>();
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));//获得文件流
String line = null;
while ((line = br.readLine()) != null) {//读取一行
list.add(line.replaceAll("'","").split(","));
}
} finally {
if (br != null) {
br.close();
}
}
return list;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
private File cvsFile;
public CSVReader() {
}
public CSVReader(File cvsFile, File logFile) {
this.cvsFile = cvsFile;
}
public File getCvsFile() {
return cvsFile;
}
public void setcvsFile(File cvsFile) {
this.cvsFile = cvsFile;
}
public void load(Loader loader) {
try {
// 读取CVS文件
BufferedReader reader = new BufferedReader(new FileReader(this.cvsFile));
// 逐行读取,并导入数据到数据库
String line = null;
// 行号
int rownum = 1;
while ((line = reader.readLine()) != null) {
try {
loader.load(parseLine(line));
} catch(Exception e) {
}
// 行号加一
rownum ++;
}
} catch (FileNotFoundException e) {} catch (IOException e) {}
}
private static List<String> parseLine(String line) {
// 解析字符串
List<String> tokenList = new ArrayList<String>();
StringBuffer token = new StringBuffer();
// 是否在引号内
boolean inQuotation = false;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == ',' && !inQuotation) {
tokenList.add(token.toString());
token.setLength(0);
} else if (c == '"') {
if (inQuotation) {
char d = line.charAt(i+1);
if (d == '"') {
token.append(c);
i ++;
} else {
inQuotation = !inQuotation;
}
} else {
inQuotation = !inQuotation;
}
} else {
token.append(c);
}
}
tokenList.add(token.toString());
// 返回解析结果
return tokenList;
}
public static void main(String[] args) throws Exception {
CSVReader.readCSV("D:/TestFile1.csv");
File cvsFile = new File("D:/TestFile1.csv");
File logFile = new File("D:/Load.log");
CSVReader loader = new CSVReader(cvsFile, logFile);
loader.load(new Loader(){
public void load(List recFieldList) throws Exception{
for (int i = 0; i < recFieldList.size(); i++) {
System.out.println("F[" + i + "]=" + recFieldList.get(i).toString().replaceAll("'", ""));
}
}
});
}
public static List<String[]> readCSV(String filepath) throws IOException {
//filepath ="D:/TestFile1.csv"
File file = new File(filepath);//到目录下取文件
BufferedReader br = null;
List<String[]> list = new ArrayList<String[]>();
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));//获得文件流
String line = null;
while ((line = br.readLine()) != null) {//读取一行
list.add(line.replaceAll("'","").split(","));
}
} finally {
if (br != null) {
br.close();
}
}
return list;
}
}
相关文章推荐
- javaCsv解析Csv文件
- 用正则表达式和java解析csv文件
- java 解析csv文件例子,csv文件 中文乱码问题
- 【csvjdbc】Java解析csv文件的开源包及应用
- java 解析 csv 文件
- Java解析CSV文件
- 如何用Java解析CSV文件
- POI以SAX方式解析Excel2007大文件(包含空单元格的处理) Java生成CSV文件实例详解
- 使用Java解析CSV文件
- Java解析CSV文件
- CSV文件格式 解析csv格式的java函数
- Java解析CSV文件
- Java解析CSV文件
- java 解析 csv 文件
- Java CSV文件解析研究
- java读取csv文件示例分享(java解析csv文件)
- 用正则表达式和java解析csv文件
- Java列表分页查询结果导出到CSV文件,导入CSV文件并解析
- Java 解析csv文件
- 用java解析csv文件