import java.util.ArrayList;
import java.io.*;
public class ReadCSVFile {
public static ArrayList readFile(String filename) {
// BUG 000-000761
FileReader fileReader = null;
BufferedReader reader = null;
ArrayList fileList = new ArrayList();
try {
File file = new File(filename);
// BUG 000-000761
//BufferedReader reader = new BufferedReader(new FileReader(file));
fileReader = new FileReader(file);
reader = new BufferedReader(fileReader);
String strArray[] = null;
String strStr = "";
int strNum = 0;
String csvArray[] = null;
String csvStr = "";
int csvNum = 0;
ArrayList arr = null;
int num;
int cnt;
String strCSV = null;
while ((strCSV = reader.readLine()) != null) {
arr = new ArrayList();
if (strCSV.charAt(0) == '#') {
continue;
}
if (strCSV.trim().length() == 0) {
continue;
}
strArray = split(strCSV, ",");
for (strNum = 0, cnt = strArray.length; strNum < cnt; strNum++) {
strStr = strArray[strNum];
if (strStr.indexOf("/"") != -1) {
num = NumOfStr(strStr, '/"');
if (num%2 == 1) {
for (int i = strNum; i < cnt; i++) {
csvStr = csvStr + strStr + ",";
strNum = strNum + 1;
strStr = strArray[strNum];
num = NumOfStr(strStr, '/"');
if (num%2 == 1) {
csvStr = csvStr + strStr + ",";
break;
}
}
} else {
csvStr = strStr;
}
csvStr = csvStr.substring(1, csvStr.length() - 3);
csvStr = str2to1(csvStr);
} else {
csvStr = strStr;
csvNum = csvNum + 1;
}
arr.add(Converter.rtrim(csvStr.trim()));
csvStr = "";
}
fileList.add(arr);
}
} catch (Exception e) {
fileList = null;
} finally {
}
// BUG 000-000761
try {
fileReader.close();
reader.close();
} catch (Exception e) {
fileList = null;
}
return fileList;
}
private static String str2to1(String str) {
String retStr = "";
for (int i = 0, cnt = str.length(); i < cnt; i++) {
retStr = retStr + str.charAt(i);
if (str.charAt(i) == '"') {
i++;
}
}
return retStr;
}
private static int NumOfStr(String str, char findChar) {
int num = 0;
for (int i = 0, cnt = str.length(); i < cnt; i++) {
if (str.charAt(i) == findChar) {
num++;
}
}
return num;
}
private static String[] split(String src, String regex) {
StringBuffer strBuf = new StringBuffer(src);
strBuf = strBuf.append(regex).append("x");
String[] tmp = strBuf.toString().split(regex);
String[] result = new String[tmp.length - 1];
for (int i = 0; i < tmp.length - 1; i++) {
result[i] = tmp[i];
}
return result;
}
}