Energy of Love |
|
|||
日歷
統計
導航常用鏈接留言簿隨筆分類
隨筆檔案
搜索最新評論
閱讀排行榜評論排行榜 |
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 panhf2003 * @version 2008/09/05, */ public class CsvFileUtil { /** * 構造,禁止實例化 */ private CsvFileUtil() {} public static void main(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文件讀取<BR/> 讀取絕對路徑為argPath的csv文件數據,并以List返回。 * * @param argPath * csv文件絕對路徑 * @return csv文件數據(List<String[]>) * @throws FileNotFoundException * @throws IOException */ public static List<String[]> readCsvFile(String argPath) throws FileNotFoundException, IOException { CsvFileUtil util = new CsvFileUtil(); File cvsFile = new File(argPath); List<String[]> list = new ArrayList<String[]>(); FileReader fileReader = null; BufferedReader bufferedReader = null; try { fileReader = new FileReader(cvsFile); bufferedReader = new BufferedReader(fileReader); String regExp = util.getRegExp(); String strLine = ""; String str = ""; while ((strLine = bufferedReader.readLine()) != null) { Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(strLine); List<String> listTemp = new ArrayList<String>(); 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); } } // test System.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<String[]> argList, String argPath, String argFileName, boolean isNewFile) throws IOException, Exception { CsvFileUtil util = new CsvFileUtil(); // 數據check if (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); // 文件路徑check if (!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() { StringBuffer strRegExps = new StringBuffer(); 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("*[ ]*"); return strRegExps.toString(); } private static final String SPECIAL_CHAR_A = "[^\",\\n ]"; private static final String SPECIAL_CHAR_B = "[^\",\\n]"; }
|
![]() |
|
Copyright © 不高興 | Powered by: 博客園 模板提供:滬江博客 |