讀取csv文件
package test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ReadCSV {
private InputStreamReader fr = null;
private BufferedReader br = null;
public ReadCSV(String f) throws IOException {
fr = new InputStreamReader(new FileInputStream(f), "UTF-8");
}
/**
* 解析csv文件 到一個list中 每個單元個爲一個String類型記錄,每一行爲一個list。 再將所有的行放到一個總list中
*/
public List<String> readCSVFile() throws IOException {
br = new BufferedReader(fr);
String rec = null;// 一行
List<String> listFile = new ArrayList<String>();
try {
while ((rec = br.readLine()) != null) {
//每行記錄放入一個list
/*String[] cell=rec.split(",");
List<String> cells = new ArrayList<String>();//存放 每行記錄的list
for (String str : cell) {
cells.add(str);
}
listFile.add(cells);*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
}
return listFile;
}
/**
* 把CSV文件的一行轉換成ArrayList
*/
public static ArrayList<String> fromCSVLinetoArray(String source) {
if (source == null || source.length() == 0) {
return new ArrayList<String>();
}
int currentPosition = 0;
int maxPosition = source.length();
int nextComma = 0;
ArrayList<String> rtnArray = new ArrayList<String>();
while (currentPosition < maxPosition) {
nextComma = nextComma(source, currentPosition);
if(nextComma==currentPosition){
rtnArray.add("空");
}
else{
rtnArray.add(nextToken(source, currentPosition, nextComma));
}
currentPosition = nextComma + 1;
if (currentPosition == maxPosition) {
rtnArray.add("");
}
}
return rtnArray;
}
/**
* 查詢下一個逗號的位置。
*
* @param source 文字列
* @param st 檢索開始位置
* @return 下一個逗號的位置。
*/
private static int nextComma(String source, int st) {
int maxPosition = source.length();
boolean inquote = false;
while (st < maxPosition) {
char ch = source.charAt(st);
if (!inquote && ch == ',') {
break;
} else if ('"' == ch) {
inquote = !inquote;
}
st++;
}
return st;
}
/**
* 取得下一個字符串
*/
private static String nextToken(String source, int st, int nextComma) {
StringBuffer strb = new StringBuffer();
int next = st;
while (next < nextComma) {
char ch = source.charAt(next++);
if (ch == '"') {
if ((st + 1 < next && next < nextComma) && (source.charAt(next) == '"')) {
strb.append(ch);
next++;
}
} else {
strb.append(ch);
}
}
return strb.toString();
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ReadCSV {
private InputStreamReader fr = null;
private BufferedReader br = null;
public ReadCSV(String f) throws IOException {
fr = new InputStreamReader(new FileInputStream(f), "UTF-8");
}
/**
* 解析csv文件 到一個list中 每個單元個爲一個String類型記錄,每一行爲一個list。 再將所有的行放到一個總list中
*/
public List<String> readCSVFile() throws IOException {
br = new BufferedReader(fr);
String rec = null;// 一行
List<String> listFile = new ArrayList<String>();
try {
while ((rec = br.readLine()) != null) {
//每行記錄放入一個list
/*String[] cell=rec.split(",");
List<String> cells = new ArrayList<String>();//存放 每行記錄的list
for (String str : cell) {
cells.add(str);
}
listFile.add(cells);*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
}
return listFile;
}
/**
* 把CSV文件的一行轉換成ArrayList
*/
public static ArrayList<String> fromCSVLinetoArray(String source) {
if (source == null || source.length() == 0) {
return new ArrayList<String>();
}
int currentPosition = 0;
int maxPosition = source.length();
int nextComma = 0;
ArrayList<String> rtnArray = new ArrayList<String>();
while (currentPosition < maxPosition) {
nextComma = nextComma(source, currentPosition);
if(nextComma==currentPosition){
rtnArray.add("空");
}
else{
rtnArray.add(nextToken(source, currentPosition, nextComma));
}
currentPosition = nextComma + 1;
if (currentPosition == maxPosition) {
rtnArray.add("");
}
}
return rtnArray;
}
/**
* 查詢下一個逗號的位置。
*
* @param source 文字列
* @param st 檢索開始位置
* @return 下一個逗號的位置。
*/
private static int nextComma(String source, int st) {
int maxPosition = source.length();
boolean inquote = false;
while (st < maxPosition) {
char ch = source.charAt(st);
if (!inquote && ch == ',') {
break;
} else if ('"' == ch) {
inquote = !inquote;
}
st++;
}
return st;
}
/**
* 取得下一個字符串
*/
private static String nextToken(String source, int st, int nextComma) {
StringBuffer strb = new StringBuffer();
int next = st;
while (next < nextComma) {
char ch = source.charAt(next++);
if (ch == '"') {
if ((st + 1 < next && next < nextComma) && (source.charAt(next) == '"')) {
strb.append(ch);
next++;
}
} else {
strb.append(ch);
}
}
return strb.toString();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.