From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/util/CsvParser.java | 300 ++++++++++++++++++++++++++++++------------------------------ 1 files changed, 150 insertions(+), 150 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/CsvParser.java b/fanli/src/main/java/com/yeshi/fanli/util/CsvParser.java index 267d686..69b6ca0 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/CsvParser.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/CsvParser.java @@ -1,151 +1,151 @@ -package com.yeshi.fanli.util; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * CsvParser - */ -public class CsvParser { - // Saved input CSV file pathname - private String inputCsvFile; - - // Space mark , ; : etc. - private String spaceMark = ","; - - /** - * Contructor - * - * @param inputCsvFile - */ - public CsvParser(String inputCsvFile, String spaceMark) { - this.inputCsvFile = inputCsvFile; - this.spaceMark = spaceMark; - } - - /** - * Contructor - * - * @param inputCsvFile - */ - public CsvParser(String inputCsvFile) { - this.inputCsvFile = inputCsvFile; - this.spaceMark = ","; - } - - /** - * Get parsed array from CSV file - * - * @return - */ - public Object[] getParsedArray() throws Exception { - List<List<String>> retval = new ArrayList<List<String>>(); - - String regExp = getRegExp(); - BufferedReader in = new BufferedReader(new FileReader(this.inputCsvFile)); - String strLine; - String str = ""; - - while ((strLine = in.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(spaceMark)) { - str = str.substring(0, str.length() - 1); - str = str.trim(); - } - - if (str.startsWith("\"") && str.endsWith("\"")) { - str = str.substring(1, str.length() - 1); - if (CsvParser.isExisted("\"\"", str)) { - str = str.replaceAll("\"\"", "\""); - } - } - - if (!"".equals(str)) { - listTemp.add(str); - } - } - - // Add to retval - retval.add(listTemp); - } - in.close(); - - return retval.toArray(); - } - - /** - * Regular Expression for CSV parse - * - * @return - */ - private String getRegExp() { - // TODO 鏈畬 - - StringBuffer strRegExps = new StringBuffer(); - - return strRegExps.toString(); - } - - /** - * If argChar is exist in argStr - * - * @param argChar - * @param argStr - * @return - */ - private static boolean isExisted(String argChar, String argStr) { - - boolean blnReturnValue = false; - if ((argStr.indexOf(argChar) >= 0) && (argStr.indexOf(argChar) <= argStr.length())) { - blnReturnValue = true; - } - return blnReturnValue; - } - - /** - * Test - * - * @param args - * @throws Exception - */ - public static void main(String[] args) throws Exception { - CsvParser parser = new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test CSV Files\\dummydata_not quoted_1.csv"); - // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test - // CSV Files\\dummydata_not quoted_2.csv"); - // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test - // CSV Files\\dummydata_quoted.csv"); - // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test - // CSV Files\\dummydata_quoted_2.csv"); - - // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test - // CSV Files\\dummydata_1.csv",";"); - // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test - // CSV Files\\dummydata_2.csv",":"); - - Object[] arr = parser.getParsedArray(); - // System.out.println(arr); - - for (Object obj : arr) { - System.out.print("["); - - List<String> ls = (List<String>) obj; - - for (String item : ls) { - System.out.println(item + ","); - } - - System.out.println("],"); - } - } +package com.yeshi.fanli.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * CsvParser + */ +public class CsvParser { + // Saved input CSV file pathname + private String inputCsvFile; + + // Space mark , ; : etc. + private String spaceMark = ","; + + /** + * Contructor + * + * @param inputCsvFile + */ + public CsvParser(String inputCsvFile, String spaceMark) { + this.inputCsvFile = inputCsvFile; + this.spaceMark = spaceMark; + } + + /** + * Contructor + * + * @param inputCsvFile + */ + public CsvParser(String inputCsvFile) { + this.inputCsvFile = inputCsvFile; + this.spaceMark = ","; + } + + /** + * Get parsed array from CSV file + * + * @return + */ + public Object[] getParsedArray() throws Exception { + List<List<String>> retval = new ArrayList<List<String>>(); + + String regExp = getRegExp(); + BufferedReader in = new BufferedReader(new FileReader(this.inputCsvFile)); + String strLine; + String str = ""; + + while ((strLine = in.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(spaceMark)) { + str = str.substring(0, str.length() - 1); + str = str.trim(); + } + + if (str.startsWith("\"") && str.endsWith("\"")) { + str = str.substring(1, str.length() - 1); + if (CsvParser.isExisted("\"\"", str)) { + str = str.replaceAll("\"\"", "\""); + } + } + + if (!"".equals(str)) { + listTemp.add(str); + } + } + + // Add to retval + retval.add(listTemp); + } + in.close(); + + return retval.toArray(); + } + + /** + * Regular Expression for CSV parse + * + * @return + */ + private String getRegExp() { + // TODO 鏈畬 + + StringBuffer strRegExps = new StringBuffer(); + + return strRegExps.toString(); + } + + /** + * If argChar is exist in argStr + * + * @param argChar + * @param argStr + * @return + */ + private static boolean isExisted(String argChar, String argStr) { + + boolean blnReturnValue = false; + if ((argStr.indexOf(argChar) >= 0) && (argStr.indexOf(argChar) <= argStr.length())) { + blnReturnValue = true; + } + return blnReturnValue; + } + + /** + * Test + * + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + CsvParser parser = new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test CSV Files\\dummydata_not quoted_1.csv"); + // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test + // CSV Files\\dummydata_not quoted_2.csv"); + // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test + // CSV Files\\dummydata_quoted.csv"); + // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test + // CSV Files\\dummydata_quoted_2.csv"); + + // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test + // CSV Files\\dummydata_1.csv",";"); + // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test + // CSV Files\\dummydata_2.csv",":"); + + Object[] arr = parser.getParsedArray(); + // System.out.println(arr); + + for (Object obj : arr) { + System.out.print("["); + + List<String> ls = (List<String>) obj; + + for (String item : ls) { + System.out.println(item + ","); + } + + System.out.println("],"); + } + } } \ No newline at end of file -- Gitblit v1.8.0