1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java 解析 csv 文件

java 解析 csv 文件

时间:2021-12-15 03:22:16

相关推荐

java 解析 csv 文件

文章分类:JavaEye

一。貌似有bug,不行用 二。或 三。 的方法

Java代码importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern; /* *文件规则 *Microsoft的格式是最简单的。以逗号分隔的值要么是“纯粹的”(仅仅包含在括号之前), *要么是在双引号之间(这时数据中的双引号以一对双引号表示)。 *TenThousand,10000,2710,,"10,000","It's""10Grand"",baby",10K *这一行包含七个字段(fields): *TenThousand *10000 *2710 *空字段 *10,000 *It's"10Grand",baby *10K *每条记录占一行 *以逗号为分隔符 *逗号前后的空格会被忽略 *字段中包含有逗号,该字段必须用双引号括起来。如果是全角的没有问题。 *字段中包含有换行符,该字段必须用双引号括起来 *字段前后包含有空格,该字段必须用双引号括起来 *字段中的双引号用两个双引号表示 *字段中如果有双引号,该字段必须用双引号括起来 *第一条记录,可以是字段名 */publicclassCSVAnalysis{privateInputStreamReaderfr=null;privateBufferedReaderbr=null;publicCSVAnalysis(Stringf)throwsIOException{ fr=newInputStreamReader(newFileInputStream(f)); } /** *解析csv文件到一个list中 *每个单元个为一个String类型记录,每一行为一个list。 *再将所有的行放到一个总list中 *@return *@throwsIOException */publicList<List<String>>readCSVFile()throwsIOException{ br=newBufferedReader(fr); Stringrec=null;//一行 Stringstr;//一个单元格 List<List<String>>listFile=newArrayList<List<String>>();try{ //读取一行while((rec=br.readLine())!=null){ PatternpCells=Pattern .compile("(/"[^/"]*(/"{2})*[^/"]*/")*[^,]*,"); MatchermCells=pCells.matcher(rec); List<String>cells=newArrayList<String>();//每行记录一个list //读取每个单元格while(mCells.find()){ str=mCells.group(); str=str.replaceAll( "(?sm)/"?([^/"]*(/"{2})*[^/"]*)/"?.*,","$1"); str=str.replaceAll("(?sm)(/"(/"))","$2"); cells.add(str); } listFile.add(cells); } }catch(Exceptione){ e.printStackTrace(); }finally{if(fr!=null){ fr.close(); }if(br!=null){ br.close(); } }returnlistFile; }publicstaticvoidmain(String[]args)throwsThrowable{ CSVAnalysisparser=newCSVAnalysis("c:/test2.csv"); parser.readCSVFile(); } }

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** 文件规则* Microsoft的格式是最简单的。以逗号分隔的值要么是“纯粹的”(仅仅包含在括号之前),* 要么是在双引号之间(这时数据中的双引号以一对双引号表示)。* Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K* 这一行包含七个字段(fields):*Ten Thousand*10000* 2710 *空字段*10,000*It's "10 Grand", baby*10K* 每条记录占一行* 以逗号为分隔符* 逗号前后的空格会被忽略* 字段中包含有逗号,该字段必须用双引号括起来。如果是全角的没有问题。* 字段中包含有换行符,该字段必须用双引号括起来* 字段前后包含有空格,该字段必须用双引号括起来* 字段中的双引号用两个双引号表示* 字段中如果有双引号,该字段必须用双引号括起来* 第一条记录,可以是字段名*/public class CSVAnalysis {private InputStreamReader fr = null;private BufferedReader br = null;public CSVAnalysis(String f) throws IOException {fr = new InputStreamReader(new FileInputStream(f));}/*** 解析csv文件 到一个list中* 每个单元个为一个String类型记录,每一行为一个list。* 再将所有的行放到一个总list中* @return* @throws IOException*/public List<List<String>> readCSVFile() throws IOException {br = new BufferedReader(fr);String rec = null;//一行String str;//一个单元格List<List<String>> listFile = new ArrayList<List<String>>();try {//读取一行while ((rec = br.readLine()) != null) {Pattern pCells = pile("(/"[^/"]*(/"{2})*[^/"]*/")*[^,]*,");Matcher mCells = pCells.matcher(rec);List<String> cells = new ArrayList<String>();//每行记录一个list//读取每个单元格while (mCells.find()) {str = mCells.group();str = str.replaceAll("(?sm)/"?([^/"]*(/"{2})*[^/"]*)/"?.*,", "$1");str = str.replaceAll("(?sm)(/"(/"))", "$2");cells.add(str);}listFile.add(cells);}} catch (Exception e) {e.printStackTrace();} finally {if (fr != null) {fr.close();}if (br != null) {br.close();}}return listFile;}public static void main(String[] args) throws Throwable {CSVAnalysis parser = new CSVAnalysis("c:/test2.csv");parser.readCSVFile();}}

二。

在解析csv文件之前,先来看看什么是csv文件以及csv文件的格式。

csv(Comma Separate Values)文件即逗号分隔符文件,它是一种文本文件,可以直接以文本打开,以逗号分隔。windows默认用excel打开。它的格式包括以下几点(它的格式最好就看excel是如何解析的。):

①每条记录占一行;

②以逗号为分隔符;

③逗号前后的空格会被忽略;

④字段中包含有逗号,该字段必须用双引号括起来;

⑤字段中包含有换行符,该字段必须用双引号括起来;

⑥字段前后包含有空格,该字段必须用双引号括起来;

⑦字段中的双引号用两个双引号表示;

⑧字段中如果有双引号,该字段必须用双引号括起来;

⑨第一条记录,可以是字段名;

⑩以上提到的逗号和双引号均为半角字符。

下面通过正则表达式和java解析csv文件。

首先给出匹配csv文件的一个最小单位数据的正则表达式(如:1,2,3是csv文件的一行数据,则1,是该csv文件的一个最小单位数据):

"(([^",/n ]*[,/n ])*([^",/n ]*"{2})*)*[^",/n ]*"[ ]*,[ ]*|[^",/n]*[ ]*,[ ]*|"(([^",/n ]*[,/n ])*([^",/n ]*"{2})*)*[^",/n ]*"[ ]*|[^",/n]*[ ]*

下面是解析文件的java代码:

Java代码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; /** *@authorpanhf *@version/09/05, */publicclassCsvFileUtil{privatestaticfinalStringSPECIAL_CHAR_A="[^/",//n]";privatestaticfinalStringSPECIAL_CHAR_B="[^/",//n]"; /** *构造,禁止实例化 */privateCsvFileUtil(){ }publicstaticvoidmain(String[]args){ //testtry{ readCsvFile("e://test1.csv"); }catch(FileNotFoundExceptionex){ Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,null,ex); }catch(IOExceptionex){ Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,null,ex); } } /** *csv文件读取<BR/> *读取绝对路径为argPath的csv文件数据,并以List返回。 * *@paramargPathcsv文件绝对路径 *@returncsv文件数据(List<String[]>) *@throwsFileNotFoundException *@throwsIOException */publicstaticListreadCsvFile(StringargPath)throwsFileNotFoundException,IOException{ CsvFileUtilutil=newCsvFileUtil(); FilecvsFile=newFile(argPath); Listlist=newArrayList(); FileReaderfileReader=null; BufferedReaderbufferedReader=null;try{ fileReader=newFileReader(cvsFile); bufferedReader=newBufferedReader(fileReader); StringregExp=util.getRegExp(); //test System.out.println(regExp); StringstrLine=""; Stringstr="";while((strLine=bufferedReader.readLine())!=null){ Patternpattern=pile(regExp); Matchermatcher=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)){ //test System.out.print(str+""); listTemp.add(str); } } //test System.out.println(); list.add((String[])listTemp.toArray(newString[listTemp.size()])); } }catch(FileNotFoundExceptione){throwe; }catch(IOExceptione){throwe; }finally{try{if(bufferedReader!=null){ bufferedReader.close(); }if(fileReader!=null){ fileReader.close(); } }catch(IOExceptione){throwe; } }returnlist; } /** *csv文件做成<BR/> *将argList写入argPath路径下的argFileName文件里。 * *@paramargList要写入csv文件的数据(List<String[]>) *@paramargPathcsv文件路径 *@paramargFileNamecsv文件名 *@paramisNewFile是否覆盖原有文件 *@throwsIOException *@throwsException */publicstaticvoidwriteCsvFile(ListargList,StringargPath,StringargFileName,booleanisNewFile)throwsIOException,Exception{ CsvFileUtilutil=newCsvFileUtil(); //数据checkif(argList==null||argList.size()==0){thrownewException("没有数据"); }for(inti=0;i<argList.size();i++){if(!(argList.get(i)instanceofString[])){thrownewException("数据格式不对"); } } FileWriterfileWriter=null; BufferedWriterbufferedWriter=null; StringstrFullFileName=argPath;if(strFullFileName.lastIndexOf("//")==(strFullFileName.length()-1)){ strFullFileName+=argFileName; }else{ strFullFileName+="//"+argFileName; } Filefile=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<argList.size();i++){ String[]strTemp=(String[])argList.get(i);for(intj=0;j<strTemp.length;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<strTemp.length-1){ bufferedWriter.write(","); } } bufferedWriter.newLine(); } }catch(IOExceptione){ e.printStackTrace(); }finally{try{if(bufferedWriter!=null){ bufferedWriter.close(); }if(fileWriter!=null){ fileWriter.close(); } }catch(IOExceptione){throwe; } } } /** *@paramargChar *@paramargStr *@return */privatebooleanisExisted(StringargChar,StringargStr){booleanblnReturnValue=false;if((argStr.indexOf(argChar)>=0) &&(argStr.indexOf(argChar)<=argStr.length())){ blnReturnValue=true; }returnblnReturnValue; } /** *正则表达式。 *@return匹配csv文件里最小单位的正则表达式。 */privateStringgetRegExp(){ StringstrRegExp=""; strRegExp= "/"(("+SPECIAL_CHAR_A+"*[,//n])*("+SPECIAL_CHAR_A+"*/"{2})*)*"+SPECIAL_CHAR_A+"*/"[]*,[]*"+"|"+SPECIAL_CHAR_B+"*[]*,[]*"+"|/"(("+SPECIAL_CHAR_A+"*[,//n])*("+SPECIAL_CHAR_A+"*/"{2})*)*"+SPECIAL_CHAR_A+"*/"[]*"+"|"+SPECIAL_CHAR_B+"*[]*";returnstrRegExp; } }

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** @author panhf* @version /09/05,*/public class CsvFileUtil {private static final String SPECIAL_CHAR_A = "[^/",//n ]";private static final String SPECIAL_CHAR_B = "[^/",//n]";/*** 构造,禁止实例化*/private CsvFileUtil() {}public static void main(String[] args) {// testtry {readCsvFile("e://test1.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文件读取<BR/>* 读取绝对路径为argPath的csv文件数据,并以List返回。** @param argPath csv文件绝对路径* @return csv文件数据(List<String[]>)* @throws FileNotFoundException* @throws IOException*/public static List readCsvFile(String argPath) throws FileNotFoundException, IOException {CsvFileUtil util = new CsvFileUtil();File cvsFile = new File(argPath);List list = new ArrayList();FileReader fileReader = null;BufferedReader bufferedReader = null;try {fileReader = new FileReader(cvsFile);bufferedReader = new BufferedReader(fileReader);String regExp = util.getRegExp();// testSystem.out.println(regExp);String strLine = "";String str = "";while ((strLine = bufferedReader.readLine()) != null) {Pattern pattern = pile(regExp);Matcher matcher = pattern.matcher(strLine);List listTemp = new ArrayList();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)) {//testSystem.out.print(str+" ");listTemp.add(str);}}//testSystem.out.println();list.add((String[]) listTemp.toArray(new String[listTemp.size()]));}} catch (FileNotFoundException e) {throw e;} catch (IOException e) {throw e;} finally {try {if (bufferedReader != null) {bufferedReader.close();}if (fileReader != null) {fileReader.close();}} catch (IOException e) {throw e;}}return list;}/*** csv文件做成<BR/>* 将argList写入argPath路径下的argFileName文件里。** @param argList 要写入csv文件的数据(List<String[]>)* @param argPath csv文件路径* @param argFileName csv文件名* @param isNewFile 是否覆盖原有文件* @throws IOException* @throws Exception*/public static void writeCsvFile(List argList, String argPath, String argFileName, boolean isNewFile)throws IOException, Exception {CsvFileUtil util = new CsvFileUtil();// 数据checkif (argList == null || argList.size() == 0) {throw new Exception("没有数据");}for (int i = 0; i < argList.size(); i++) {if (!(argList.get(i) instanceof String[])) {throw new Exception("数据格式不对");}}FileWriter fileWriter = null;BufferedWriter bufferedWriter = null;String strFullFileName = argPath;if (strFullFileName.lastIndexOf("//") == (strFullFileName.length() - 1)) {strFullFileName += argFileName;} else {strFullFileName += "//" + argFileName;}File file = new File(strFullFileName);// 文件路径checkif (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}try {if (isNewFile) {// 覆盖原有文件fileWriter = new FileWriter(file);} else {// 在原有文件上追加数据fileWriter = new FileWriter(file, true);}bufferedWriter = new BufferedWriter(fileWriter);for (int i = 0; i < argList.size(); i++) {String[] strTemp = (String[]) argList.get(i);for (int j = 0; j < strTemp.length; j++) {if (util.isExisted("/"",strTemp[j])) {strTemp[j] = strTemp[j].replaceAll("/"", "/"/"");bufferedWriter.write("/""+strTemp[j]+"/"");} else if (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 < strTemp.length - 1) {bufferedWriter.write(",");}}bufferedWriter.newLine();}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedWriter != null) {bufferedWriter.close();}if (fileWriter != null) {fileWriter.close();}} catch (IOException e) {throw e;}}}/*** @param argChar* @param argStr* @return*/private boolean isExisted(String argChar, String argStr) {boolean blnReturnValue = false;if ((argStr.indexOf(argChar) >= 0)&& (argStr.indexOf(argChar) <= argStr.length())) {blnReturnValue = true;}return blnReturnValue;}/*** 正则表达式。* @return 匹配csv文件里最小单位的正则表达式。*/private String getRegExp() {String strRegExp = "";strRegExp ="/"(("+ SPECIAL_CHAR_A + "*[,//n ])*("+ SPECIAL_CHAR_A + "*/"{2})*)*"+ SPECIAL_CHAR_A + "*/"[ ]*,[ ]*"+"|"+ SPECIAL_CHAR_B + "*[ ]*,[ ]*"+ "|/"(("+ SPECIAL_CHAR_A + "*[,//n ])*("+ SPECIAL_CHAR_A + "*/"{2})*)*"+ SPECIAL_CHAR_A + "*/"[ ]*"+ "|"+ SPECIAL_CHAR_B + "*[ ]*";return strRegExp;}}

三。

该解析算法的解析规则与excel或者wps大致相同。另外包含去掉注释的方法。

构建方法该类包含一个构建方法,参数为要读取的csv文件的文件名(包含绝对路径)。

普通方法:

getVContent():一个得到当前行的值向量的方法。如果调用此方法前未调用readCSVNextRecord方法,则将返回Null。

getLineContentVector():一个得到下一行值向量的方法。如果该方法返回Null,则说明已经读到文件末尾。

close():关闭流。该方法为调用该类后应该被最后调用的方法。

readCSVNextRecord():该方法读取csv文件的下一行,如果该方法已经读到了文件末尾,则返回false;

readAtomString(String):该方法返回csv文件逻辑一行的第一个值,和该逻辑行第一个值后面的内容,如果该内容以逗号开始,则已经去掉了该逗号。这两个值以一个二维数组的方法返回。

isQuoteAdjacent(String):判断一个给定字符串的引号是否两两相邻。如果两两相邻,返回真。如果该字符串不包含引号,也返回真。

readCSVFileTitle():该方法返回csv文件中的第一行——该行不以#号开始(包括正常解析后的#号),且该行不为空

Java代码importjava.io.BufferedReader;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.Vector;publicclassCsvParse{ //声明读取流privateBufferedReaderinStream=null; //声明返回向量privateVector<String>vContent=null; /** *构建方法,参数为csv文件名<br> *如果没有找到文件,则抛出异常<br> *如果抛出异常,则不能进行页面的文件读取操作 */publicCsvParse(StringcsvFileName)throwsFileNotFoundException{ inStream=newBufferedReader(newFileReader(csvFileName)); } /** *返回已经读取到的一行的向量 *@returnvContent */publicVector<String>getVContent(){returnthis.vContent; } /** *读取下一行,并把该行的内容填充入向量中<br> *返回该向量<br> *@returnvContent装载了下一行的向量 *@throwsIOException *@throwsException */publicVector<String>getLineContentVector()throwsIOException,Exception{if(this.readCSVNextRecord()){returnthis.vContent; }returnnull; } /** *关闭流 */publicvoidclose(){if(inStream!=null){try{ inStream.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } /** *调用此方法时应该确认该类已经被正常初始化<br> *该方法用于读取csv文件的下一个逻辑行<br> *读取到的内容放入向量中<br> *如果该方法返回了false,则可能是流未被成功初始化<br> *或者已经读到了文件末尾<br> *如果发生异常,则不应该再进行读取 *@return返回值用于标识是否读到文件末尾 *@throwsException */publicbooleanreadCSVNextRecord()throwsIOException,Exception{ //如果流未被初始化则返回falseif(inStream==null){returnfalse; } //如果结果向量未被初始化,则初始化if(vContent==null){ vContent=newVector<String>(); } //移除向量中以前的元素 vContent.removeAllElements(); //声明逻辑行 StringlogicLineStr=""; //用于存放读到的行 StringBuilderstrb=newStringBuilder(); //声明是否为逻辑行的标志,初始化为falsebooleanisLogicLine=false;try{while(!isLogicLine){ StringnewLineStr=inStream.readLine();if(newLineStr==null){ strb=null; vContent=null; isLogicLine=true;break; }if(newLineStr.startsWith("#")){ //去掉注释continue; }if(!strb.toString().equals("")){ strb.append("/r/n"); } strb.append(newLineStr); StringoldLineStr=strb.toString();if(oldLineStr.indexOf(",")==-1){ //如果该行未包含逗号if(containsNumber(oldLineStr,"/"")%2==0){ //如果包含偶数个引号 isLogicLine=true;break; }else{if(oldLineStr.startsWith("/"")){if(oldLineStr.equals("/"")){continue; }else{ StringtempOldStr=oldLineStr.substring(1);if(isQuoteAdjacent(tempOldStr)){ //如果剩下的引号两两相邻,则不是一行continue; }else{ //否则就是一行 isLogicLine=true;break; } } } } }else{ //quotes表示复数的quote StringtempOldLineStr=oldLineStr.replace("/"/"","");intlastQuoteIndex=tempOldLineStr.lastIndexOf("/"");if(lastQuoteIndex==0){continue; }elseif(lastQuoteIndex==-1){ isLogicLine=true;break; }else{ tempOldLineStr=tempOldLineStr.replace("/",/"",""); lastQuoteIndex=tempOldLineStr.lastIndexOf("/"");if(lastQuoteIndex==0){continue; }if(tempOldLineStr.charAt(lastQuoteIndex-1)==','){continue; }else{ isLogicLine=true;break; } } } } }catch(IOExceptionioe){ ioe.printStackTrace(); //发生异常时关闭流if(inStream!=null){ inStream.close(); }throwioe; }catch(Exceptione){ e.printStackTrace(); //发生异常时关闭流if(inStream!=null){ inStream.close(); }throwe; }if(strb==null){ //读到行尾时为返回returnfalse; } //提取逻辑行 logicLineStr=strb.toString();if(logicLineStr!=null){ //拆分逻辑行,把分离出来的原子字符串放入向量中while(!logicLineStr.equals("")){ String[]ret=readAtomString(logicLineStr); StringatomString=ret[0]; logicLineStr=ret[1]; vContent.add(atomString); } }returntrue; } /** *读取一个逻辑行中的第一个字符串,并返回剩下的字符串<br> *剩下的字符串中不包含第一个字符串后面的逗号<br> *@paramlineStr一个逻辑行 *@return第一个字符串和剩下的逻辑行内容 */publicString[]readAtomString(StringlineStr){ StringatomString="";//要读取的原子字符串 StringorgString="";//保存第一次读取下一个逗号时的未经任何处理的字符串 String[]ret=newString[2];//要返回到外面的数组booleanisAtom=false;//是否是原子字符串的标志 String[]commaStr=lineStr.split(",");while(!isAtom){for(Stringstr:commaStr){if(!atomString.equals("")){ atomString=atomString+","; } atomString=atomString+str; orgString=atomString;if(!isQuoteContained(atomString)){ //如果字符串中不包含引号,则为正常,返回 isAtom=true;break; }else{if(!atomString.startsWith("/"")){ //如果字符串不是以引号开始,则表示不转义,返回 isAtom=true;break; }elseif(atomString.startsWith("/"")){ //如果字符串以引号开始,则表示转义if(containsNumber(atomString,"/"")%2==0){ //如果含有偶数个引号 Stringtemp=atomString;if(temp.endsWith("/"")){ temp=temp.replace("/"/"","");if(temp.equals("")){ //如果temp为空 atomString=""; isAtom=true;break; }else{ //如果temp不为空,则去掉前后引号 temp=temp.substring(1,temp .lastIndexOf("/""));if(temp.indexOf("/"")>-1){ //去掉前后引号和相邻引号之后,若temp还包含有引号 //说明这些引号是单个单个出现的 temp=atomString; temp=temp.substring(1); temp=temp.substring(0,temp .indexOf("/"")) +temp.substring(temp .indexOf("/"")+1); atomString=temp; isAtom=true;break; }else{ //正常的csv文件 temp=atomString; temp=temp.substring(1,temp .lastIndexOf("/"")); temp=temp.replace("/"/"","/""); atomString=temp; isAtom=true;break; } } }else{ //如果不是以引号结束,则去掉前两个引号 temp=temp.substring(1,temp.indexOf('/"',1)) +temp .substring(temp .indexOf('/"',1)+1); atomString=temp; isAtom=true;break; } }else{ //如果含有奇数个引号 //TODO处理奇数个引号的情况if(!atomString.equals("/"")){ StringtempAtomStr=atomString.substring(1);if(!isQuoteAdjacent(tempAtomStr)){ //这里做的原因是,如果判断前面的字符串不是原子字符串的时候就读取第一个取到的字符串 //后面取到的字符串不计入该原子字符串 tempAtomStr=atomString.substring(1);inttempQutoIndex=tempAtomStr .indexOf("/""); //这里既然有奇数个quto,所以第二个quto肯定不是最后一个 tempAtomStr=tempAtomStr.substring(0, tempQutoIndex) +tempAtomStr .substring(tempQutoIndex+1); atomString=tempAtomStr; isAtom=true;break; } } } } } } } //先去掉之前读取的原字符串的母字符串if(lineStr.length()>orgString.length()){ lineStr=lineStr.substring(orgString.length()); }else{ lineStr=""; } //去掉之后,判断是否以逗号开始,如果以逗号开始则去掉逗号if(lineStr.startsWith(",")){if(lineStr.length()>1){ lineStr=lineStr.substring(1); }else{ lineStr=""; } } ret[0]=atomString; ret[1]=lineStr;returnret; } /** *该方法取得父字符串中包含指定字符串的数量<br> *如果父字符串和字字符串任意一个为空值,则返回零 *@paramparentStr *@paramparameter *@return */publicintcontainsNumber(StringparentStr,Stringparameter){intcontainNumber=0;if(parentStr==null||parentStr.equals("")){return0; }if(parameter==null||parameter.equals("")){return0; }for(inti=0;i<parentStr.length();i++){ i=parentStr.indexOf(parameter,i);if(i>-1){ i=i+parameter.length(); i--; containNumber=containNumber+1; }else{break; } }returncontainNumber; } /** *该方法用于判断给定的字符串中的引号是否相邻<br> *如果相邻返回真,否则返回假<br> * *@paramp_String *@return */publicbooleanisQuoteAdjacent(Stringp_String){booleanret=false; Stringtemp=p_String; temp=temp.replace("/"/"","");if(temp.indexOf("/"")==-1){ ret=true; } //TODO引号相邻returnret; } /** *该方法用于判断给定的字符串中是否包含引号<br> *如果字符串为空或者不包含返回假,包含返回真<br> * *@paramp_String *@return */publicbooleanisQuoteContained(Stringp_String){booleanret=false;if(p_String==null||p_String.equals("")){returnfalse; }if(p_String.indexOf("/"")>-1){ ret=true; }returnret; } /** *读取文件标题 * *@return正确读取文件标题时返回true,否则返回false *@throwsException *@throwsIOException */publicbooleanreadCSVFileTitle()throwsIOException,Exception{ StringstrValue="";booleanisLineEmpty=true;do{if(!readCSVNextRecord()){returnfalse; }if(vContent.size()>0){ strValue=(String)vContent.get(0); }for(Stringstr:vContent){if(str!=null&&!str.equals("")){ isLineEmpty=false;break; } } //csv文件中前面几行以#开头为注释行 }while(strValue.trim().startsWith("#")||isLineEmpty);returntrue; } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。