项目中经常需要将数据批量导入数据库,避免用户繁重的手工输入工作。数据的批量导入,可以使用CVS格式,这种格式的文件可以通过Excel很方便进行输入,又不会因为格式问题导致导入错误。CVS文件的导入分以下两步:
1. 上传CVS文件
将CVS文件存贮到固定的位置,供解析时使用
2. 解析CVS文件
1. 上传CVS文件
将CVS文件存贮到固定的位置,供解析时使用
2. 解析CVS文件
将CVS文件当作文本文件处理,逐行读取CVS文件,然后进行解析,并将解析结果根据实际业务进行处理,记录处理日志到日志文件。
import java.util.List;
/**
* 逗号分割文件行数据加载接口
* @version 1.0 2011-02-17
* @author zhanzhengqiang
*/
public interface Loader {
/**
* 加载文件一行数据
* @param recFiledList 数据列表
* @throws Exception 加载数据异常
*/
public void load(List recFiledList) throws Exception;
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
/**
* 逗号分割文件数据加载类
* @version 1.0
* @author zhanzhengqiang
*/
public class CVSFileLoader {
/**
* 加载cvs文件路径
*/
private File cvsFile;
/**
* 加载数据日志文件路径
*/
private File logFile;
/**
* 默认构造函数
*/
public CVSFileLoader() {
}
/**
* 构造函数
* @param cvsFile 加载cvs文件路径
* @param logFile 加载数据日志文件路径
* @param loader
*/
public CVSFileLoader(File cvsFile, File logFile) {
this.cvsFile = cvsFile;
this.logFile = logFile;
// 准备日志文件
this.prepareLogFile();
}
/**
* @return the cvsFile
*/
public File getCvsFile() {
return cvsFile;
}
/**
* @param cvsFile the cvsFile to set
*/
public void setcvsFile(File cvsFile) {
this.cvsFile = cvsFile;
}
/**
* @return the logFile
*/
public File getLogFile() {
return logFile;
}
/**
* @param logFile the logFile to set
*/
public void setLogFile(File logFile) {
this.logFile = logFile;
// 准备日志文件
this.prepareLogFile();
}
/**
* 加载CVS文件
* @param loader 文件加载器
* @throws Exception 加载异常
*/
public void load(Loader loader) {
this.writeLog("#导入数据开始#");
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));
this.writeLog("第" + rownum + "行:[" + line + "]导入成功");
} catch(Exception e) {
this.writeLog("第" + rownum + "行:[" + line + "]导入出错:" + e.getMessage());
}
// 行号加一
rownum ++;
}
} catch (FileNotFoundException e) {
this.writeLog("要导入的文件[" + cvsFile.getAbsolutePath() + "]找不到");
} catch (IOException e) {
this.writeLog("导入数据出错:" + e.getMessage());
}
this.writeLog("#导入数据结束#");
}
/**
* 按逗号分割行
* @param line 逗号分割文件行
* @return 分割结果列表
*/
private static List parseLine(String line) {
// 解析字符串
List tokenList = new ArrayList();
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;
}
/**
* 准备日志文件
*/
private void prepareLogFile() {
if (this.logFile == null) {
return;
}
// 删除原日志文件
if (this.logFile.isFile() && this.logFile.exists()) {
this.logFile.delete();
}
// 创建日志目录
File logDir = this.logFile.getParentFile();
if (!logDir.exists()) {
logDir.mkdirs();
}
}
/**
* 输出数据加载日志到日志文件
* @param msg 日志信息
*/
private void writeLog(String msg) {
// 没有指定日志文件,则直接返回
if (this.logFile == null) {
return;
}
// 定义日志文件
RandomAccessFile writeLogFile = null;
try {
writeLogFile = new RandomAccessFile(this.logFile, "rw");
// 定位到文件末尾并换行
if (writeLogFile.length() > 0) {
writeLogFile.seek(logFile.length());
writeLogFile.writeBytes(System.getProperty("line.separator"));
}
writeLogFile.write(msg.getBytes("GBK"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writeLogFile != null) {
writeLogFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.List;
/** 测试类 */
public class ReadCSVFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File cvsFile = new File("D:/TestFile.csv");
File logFile = new File("D:/Load.log");
CVSFileLoader loader = new CVSFileLoader(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));
}
}
});
}
}
以上代码只是示例,没有涉及实际业务,实际项目中需要根据实际业务对数据进行处理。