操作CSV文件的一些方法

package com.dianping.poi.dedup.util;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Map;


import org.apache.commons.beanutils.PropertyUtils;


import com.dianping.poi.dedup.model.Poi;


import au.com.bytecode.opencsv.CSVWriter;


public class PoiCsvWriter {


private CSVWriter _writer;


public PoiCsvWriter(String filePath) throws FileNotFoundException {
OutputStreamWriter char_output = new OutputStreamWriter(
new FileOutputStream(filePath), Charset.forName("UTF-8")
.newEncoder());
_writer = new CSVWriter(char_output, '\t', CSVWriter.NO_QUOTE_CHARACTER);
}


public void writeNext(String[] nextLine) {
for (int i = 0; i < nextLine.length; i++) {
if (nextLine[i] != null) {
nextLine[i] = nextLine[i].replaceAll("[\t\n]", " ").replaceAll(
"\\\\N", "");
}
}
_writer.writeNext(nextLine);
}


public void flush() throws IOException {
_writer.flush();
}


public void close() throws IOException {
_writer.close();
}

public static String[] getCsvLineFromPoi(Poi p, String[] fields) throws Exception {
       String[] ret = new String[fields.length];
       Map<String, String> extendedInfo = p.getExtendedInfo();
       for (int i = 0; i < fields.length; i++){
           String fieldName = unCapitalizeFirstLetter(fields[i]);
//            System.out.println(" i :" +i );
           try {
               if (PropertyUtils.getProperty(p, fieldName) != null){
                   ret[i] = PropertyUtils.getProperty(p, fieldName).toString();
               } else if (extendedInfo != null && extendedInfo.containsKey(fields[i])) {
                   ret[i] = extendedInfo.get(fields[i]);
               } else {
                   ret[i] = null;
               }
           } catch (NoSuchMethodException e) {
               if (extendedInfo != null && !extendedInfo.isEmpty()) {
                   ret[i] = extendedInfo.get(fields[i]);
               }
           }
       }


       return ret;
   }
 
private static String unCapitalizeFirstLetter(String input){
       return input.substring(0, 1).toLowerCase() + input.substring(1);
   }




public static void main(String[] args) {
// TODO Auto-generated method stub


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值