java解析.csv文件,java解析CSV文件

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 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;     }          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  readCSV(String filepath) throws IOException {         //filepath ="D:/TestFile1.csv"         File file = new File(filepath);//到目录下取文件         BufferedReader br = null;         List list = new ArrayList();         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;     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值