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;
public class AnalysisCsv
{
private static final String SPECIAL_CHAR_A = "[^/",//n ]";
private static final String SPECIAL_CHAR_B = "[^/",//n]";
/**
* 构造,禁止实例化
*/
private AnalysisCsv()
{
}
public static void main(String[]args)
{
try
{
readCsvFile("D://Eclipseworkspace//AnalysisCsv//江苏省高邮中学2003届毕业生名单.csv");
}
catch (FileNotFoundException ex)
{
Logger.getLogger(AnalysisCsv.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(AnalysisCsv.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
{
AnalysisCsv util = new AnalysisCsv();
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();
System.out.println(regExp);
String strLine = "";
String str = "";
while ((strLine = bufferedReader.readLine()) != null)
{
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(strLine);
List list2 = 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))
{
System.out.print(str+" ");
//listTemp.add(str);
}
list.add(str);
}
list2.add("a");
list2.add("b");
list2.add("c");
list.clear();
}
}
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;
}
/**
* @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;
}
}