importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.logging.Level;importjava.util.logging.Logger;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/***@authorpanhf2003
*@version2008/09/05,*/publicclassCsvFileUtil
{/*** 构造,禁止实例化*/privateCsvFileUtil()
{}publicstaticvoidmain(String[] args)
{try{
readCsvFile("d:\\ZD_CUSTOMER_VIEW.csv");
}catch(FileNotFoundException ex)
{
Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,null, ex);
}catch(IOException ex)
{
Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,null, ex);
}
}/*** csv文件读取
读取绝对路径为argPath的csv文件数据,并以List返回。
*
*@paramargPath
* csv文件绝对路径
*@returncsv文件数据(List)
*@throwsFileNotFoundException
*@throwsIOException*/publicstaticListreadCsvFile(String argPath)throwsFileNotFoundException, IOException
{
CsvFileUtil util=newCsvFileUtil();
File cvsFile=newFile(argPath);
Listlist=newArrayList();
FileReader fileReader=null;
BufferedReader bufferedReader=null;try{
fileReader=newFileReader(cvsFile);
bufferedReader=newBufferedReader(fileReader);
String regExp=util.getRegExp();
String strLine="";
String str="";while((strLine=bufferedReader.readLine())!=null)
{
Pattern pattern=Pattern.compile(regExp);
Matcher matcher=pattern.matcher(strLine);
ListlistTemp=newArrayList();while(matcher.find())
{
str=matcher.group();
str=str.trim();if(str.endsWith(","))
{
str=str.substring(0, str.length()-1);
str=str.trim();
}if(str.startsWith("\"") && str.endsWith("\""))
{
str=str.substring(1, str.length()-1);if(util.isExisted("\"\"", str))
{
str=str.replaceAll("\"\"","\"");}
}if(!"".equals(str))
{
System.out.print(str+"");
listTemp.add(str);
}
}//testSystem.out.println();
list.add((String[]) listTemp
.toArray(newString[listTemp.size()]));
}
}catch(FileNotFoundException e)
{throwe;
}catch(IOException e)
{throwe;
}finally{try{if(bufferedReader!=null)
{
bufferedReader.close();
}if(fileReader!=null)
{
fileReader.close();
}
}catch(IOException e)
{throwe;
}
}returnlist;
}/*** csv文件做成
将argList写入argPath路径下的argFileName文件里。
*
*@paramargList
* 要写入csv文件的数据(List)
*@paramargPath
* csv文件路径
*@paramargFileName
* csv文件名
*@paramisNewFile
* 是否覆盖原有文件
*@throwsIOException
*@throwsException*/publicstaticvoidwriteCsvFile(ListargList, String argPath,
String argFileName,booleanisNewFile)throwsIOException,
Exception
{
CsvFileUtil util=newCsvFileUtil();//数据checkif(argList==null||argList.size()==0)
{thrownewException("没有数据");
}for(inti=0; i
{if(!(argList.get(i)instanceofString[]))
{thrownewException("数据格式不对");
}
}
FileWriter fileWriter=null;
BufferedWriter bufferedWriter=null;
String strFullFileName=argPath;if(strFullFileName.lastIndexOf("\\")==(strFullFileName.length()-1))
{
strFullFileName+=argFileName;
}else{
strFullFileName+="\\"+argFileName;
}
File file=newFile(strFullFileName);//文件路径checkif(!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}try{if(isNewFile)
{//覆盖原有文件fileWriter=newFileWriter(file);
}else{//在原有文件上追加数据fileWriter=newFileWriter(file,true);
}
bufferedWriter=newBufferedWriter(fileWriter);for(inti=0; i
{
String[] strTemp=(String[]) argList.get(i);for(intj=0; j
{if(util.isExisted("\"", strTemp[j])){
strTemp[j]=strTemp[j].replaceAll("\"","\"\"");bufferedWriter.write("\""+ strTemp[j] +"\"");
}elseif(util.isExisted(",", strTemp[j])||util.isExisted("\n", strTemp[j])||util.isExisted("", strTemp[j])||util.isExisted("??", strTemp[j]))
{
bufferedWriter.write("\""+ strTemp[j] +"\"");
}else{
bufferedWriter.write(strTemp[j]);
}if(j
{
bufferedWriter.write(",");
}
}
bufferedWriter.newLine();
}
}catch(IOException e)
{
e.printStackTrace();
}finally{try{if(bufferedWriter!=null)
{
bufferedWriter.close();
}if(fileWriter!=null)
{
fileWriter.close();
}
}catch(IOException e)
{throwe;
}
}
}/***@paramargChar
*@paramargStr
*@return*/privatebooleanisExisted(String argChar, String argStr)
{booleanblnReturnValue=false;if((argStr.indexOf(argChar)>=0)&&(argStr.indexOf(argChar)<=argStr.length()))
{
blnReturnValue=true;
}returnblnReturnValue;
}/*** 正则表达式。
*
*@return匹配csv文件里最小单位的正则表达式。*/privateString getRegExp()
{
StringBuffer strRegExps=newStringBuffer();
strRegExps.append("\"((");strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*[,\\n ])*(");
strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*\"{2})*)*");strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*\"[ ]*,[ ]*");strRegExps.append("|");
strRegExps.append(SPECIAL_CHAR_B);
strRegExps.append("*[ ]*,[ ]*");
strRegExps.append("|\"((");strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*[,\\n ])*(");
strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*\"{2})*)*");strRegExps.append(SPECIAL_CHAR_A);
strRegExps.append("*\"[ ]*");strRegExps.append("|");
strRegExps.append(SPECIAL_CHAR_B);
strRegExps.append("*[ ]*");returnstrRegExps.toString();
}privatestaticfinalString SPECIAL_CHAR_A="[^\",\\n ]";privatestaticfinalString SPECIAL_CHAR_B="[^\",\\n]";}