public class Mytest {
public static List parse(String str1) {
List list = new ArrayList();
Pattern p = Pattern.compile("\"([.]+)\"|([^,]+)");
Matcher m = p.matcher(str1);
while (m.find()) {
String data = m.group();
if (data != null && !"".equals(data)) {
if (data.charAt(0) == '\"'
&& data.charAt(data.length() - 1) == '\"') {
data = data.substring(1, data.length() - 1);
}
} else {
data = "";
}
list.add(data);
}
return list;
} public static void main(String[] args) throws Throwable { BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(new File("c:\\tmp\\in.csv"))));
List resultList = new ArrayList();
while (reader.ready()) {
String str = reader.readLine();
resultList.add(parse(str));
} for (int i = 0; i < resultList.size(); i++) {
List tempList = (ArrayList) resultList.get(i);
for (int j = 0; j < tempList.size(); j++) {
System.out.println(
"data[" + i + "][" + j + "] = " + tempList.get(j));
}
}
}
}我没怎么仔细测过,大概功能能用,自己再改改吧
public static List parse(String str1) {
List list = new ArrayList();
Pattern p = Pattern.compile("\"([.]+)\"|([^,]+)");
Matcher m = p.matcher(str1);
while (m.find()) {
String data = m.group();
if (data != null && !"".equals(data)) {
if (data.charAt(0) == '\"'
&& data.charAt(data.length() - 1) == '\"') {
data = data.substring(1, data.length() - 1);
}
} else {
data = "";
}
list.add(data);
}
return list;
} public static void main(String[] args) throws Throwable { BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(new File("c:\\tmp\\in.csv"))));
List resultList = new ArrayList();
while (reader.ready()) {
String str = reader.readLine();
resultList.add(parse(str));
} for (int i = 0; i < resultList.size(); i++) {
List tempList = (ArrayList) resultList.get(i);
for (int j = 0; j < tempList.size(); j++) {
System.out.println(
"data[" + i + "][" + j + "] = " + tempList.get(j));
}
}
}
}我没怎么仔细测过,大概功能能用,自己再改改吧
解决方案 »
- Java桌面程序安装部署方案
- java applet在关闭一个窗口时调用哪个方法stop还是destroy?
- 0xFFFFFFF1是什么意思?
- 小問題問一下
- 请大帮我看下这个问题我弄了半天了,是书上的练习题,愚闷啊!
- 请教一个HashMap的问题
- 大家经常用output.flush()吧 你知不知道flush()方法竟然是个空方法
- 如何获取所连接到的数据库中的所用数据表的表名,并将这些表名显示在下拉框中?
- 有好一点的财会论坛吗?搜了半天也没到1个,能请教财务知识的那种,想转行了
- 请问在JBuilder中怎样连接MS SQLServer数据库?
- 新手看书没看懂,请各位大哥帮解释一下,好么?
- java文件读取大揭秘
data[1][0] = d
data[1][1] = "e
data[1][2] = e"
data[1][3] = f是吧?
1,"2,3",4
a,"bbb",c
d,"e
f",g
,h,i
j,k,输出应该是
data[0][0] = 1
data[0][1] = 2,3
data[0][2] = 4data[1][0] = a
data[1][1] = bb
data[1][2] = cdata[2][0] = d
data[2][1] = e
f
data[2][2] = gdata[3][0] =
data[3][1] = h
data[3][2] = idata[4][0] = j
data[4][1] = k
data[4][2] =
应该是bbb
你要做我给你分。再加一组测试例
d,"
f",g
输出
data[2][0] = d
data[2][1] =
f
data[2][2] = g
whyxx(风之子) 的程序这组数据会有
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -1
at java.lang.String.substring(String.java:1480)
at Mytest.parse(Mytest.java:16)
at Mytest.main(Mytest.java:35)
://expert.csdn.net/Expert/topic/2184/2184201.xml?temp=.9930841
我改了一下已经可以正确读取一行里的一项一项数据了。现在我不会正确读取行, 用readLine();肯定是不对
它碰到\r\n就认为是下一行了。我也在写, 对我来说这个不简单。或者我还没有想到。
就是a,,b我把中间的空字符串给漏了import java.util.regex.*;
import java.util.*;
import java.io.*;public class CSVParser { private static final String C_L_M = "\r\n";
public static ArrayList parseCSV(String filename) {
ArrayList outData = new ArrayList();
try {
BufferedReader in = new BufferedReader(new FileReader((filename)));
StringBuffer buffer = new StringBuffer();
while (in.ready()) {
String strLine = in.readLine();
buffer.append(new String(strLine.getBytes()));//, "ISO8859-1"));
buffer.append(C_L_M);
}
String allfilestring = buffer.toString();
Pattern p = Pattern.compile(".*\"[^\"]*" + C_L_M + "[^\"]*\",.*" + C_L_M + "|([^" + C_L_M + "]+)");
Matcher m = p.matcher(allfilestring);
String str;
while(m.find()) {
str = m.group();
if (str.endsWith(C_L_M)) {
str = str.substring(0, str.length() - C_L_M.length());
}
//System.out.println("'" + str + "'");
outData.add(split2Cells(str));
}
} catch (java.io.FileNotFoundException e) {
System.err.println("File not Find");
} finally {
return outData;
}
}
private static ArrayList split2Cells(String str1) {
ArrayList list = new ArrayList();
Pattern p = Pattern.compile("\"([^\"]+)\",|([^,]+)");
Matcher m = p.matcher(str1);
String str;
while(m.find()) {
str = m.group();
if (str.endsWith(",")) {
str = str.substring(0, str.length() - 1);
}
if (str.endsWith("\"") && str.startsWith("\"")) {
str = str.substring(1, str.length() - 1);
}
list.add(str);
//System.out.println("'" + str + "'");
}
return list;
} public static void main(String[] args) throws Throwable {
ArrayList data = parseCSV("in.csv");
for (int i = 0; i < data.size(); i++) {
ArrayList rowdata = (ArrayList)(data.get(i));
for (int j = 0; j < rowdata.size(); j++) {
System.out.println("data[" + i + "][" + j + "] = " + rowdata.get(j));
}
}
}
}
import java.util.*;
import java.io.*;public class CSVParser { private StringBuffer buf = null; public ArrayList parse() {
String str;
ArrayList rows = new ArrayList();
Pattern pRows = Pattern.compile("[^\\x0d]+");
Pattern pCells = Pattern.compile("(\"(.*)\",)|(([^,]*),)");
//Pattern p = Pattern.compile("([^\\x0d]+)");
Matcher mRows = pRows.matcher(buf);
//System.out.println("-----------rows------------------");
while(mRows.find()) {
//System.out.println("{" +mRows.group() + "}");
Matcher mCells = pCells.matcher(mRows.group() + ",");
ArrayList cells = new ArrayList();
while(mCells.find()) {
str = mCells.group();
//System.out.println(str);
//str = str.replaceAll("(?sm)(\"(\"))|(\"([^\"])?)|(^\\x0a(.*))", "$2");
str = str.replaceAll("(?sm)(^[\\x0a]?([^,]*)[,\\x0a]?$)", "$2");
str = str.replaceAll("(?sm)(^\"(.*)\"[,\\x0a]?$)", "$2");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
rows.add(cells);
}
return rows;
} public void readCSVFile(File CSVFile) {
int chr;
buf = new StringBuffer("");
try {
BufferedReader in = new BufferedReader(new FileReader(CSVFile));
while((chr=in.read()) != -1) {
buf.append((char)chr);
//System.out.print((char)chr);
}
in.close();
System.out.println("-------------csv file content--------------");
System.out.print(buf);
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
} public static void main(String[] args) throws Throwable {
File file = new File("Book1.csv");
CSVParser parser = new CSVParser();
parser.readCSVFile(file);
ArrayList rows = parser.parse();
int size = rows.size();
System.out.println("\n\n--------------parsed result------------------");
System.out.println("row size = " + size);
for (int i = 0; i < size; i++) {
System.out.println("rows[" + i + "] = " + rows.get(i));
ArrayList cells = (ArrayList)(rows.get(i));
System.out.println("cell size = " + cells.size());
for(int j = 0; j < cells.size(); j ++) {
System.out.println("cells[" + j + "] = " + cells.get(j));
}
}
}
}
比如有一行是
d,"e
f",g这个只是csv中的一行, 你的程序会把它认为是2行的。
d,"e
f",g
当作两行
而是解析csv文件最后会多出来一个空行,这一行可以抛弃
f",g
你就这样把它当做文本存成csv。然后用excel打开看看
import java.util.*;
import java.io.*;public class CSVParser { private StringBuffer buf = null; public ArrayList parse() {
String str;
ArrayList rows = new ArrayList();
Pattern pRows = Pattern.compile("(([^\n\"]*(\"[^\"]*(\"{2})*[^\"]*\")*)[^\n]*)\n");
Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\",)|(([^,]*),)");
Matcher mRows = pRows.matcher(buf);
//System.out.println("-----------rows------------------");
while(mRows.find()) {
//System.out.println("{" + mRows.group(1) + "}");
Matcher mCells = pCells.matcher(mRows.group(1) + ",");
ArrayList cells = new ArrayList();
while(mCells.find()) {
str = mCells.group();
System.out.println("[" + str + "]");
//str = str.replaceAll("(?sm)(\"(\"))|(\"([^\"])?)|(^\\x0a(.*))", "$2");
//str = str.replaceAll("(?sm)(^[\\x0a]?([^,]*)[,\\x0a]?$)", "$2");
str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
rows.add(cells);
}
return rows;
} public void readCSVFile(File CSVFile) {
int chr;
buf = new StringBuffer("");
try {
BufferedReader in = new BufferedReader(new FileReader(CSVFile));
while((chr=in.read()) != -1) {
buf.append((char)chr);
//System.out.print((char)chr);
}
in.close();
System.out.println("-------------csv file content--------------");
System.out.print(buf);
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
} public static void main(String[] args) throws Throwable {
File file = new File("Book3.csv");
CSVParser parser = new CSVParser();
parser.readCSVFile(file);
ArrayList rows = parser.parse();
int size = rows.size();
System.out.println("\n\n--------------parsed result------------------");
System.out.println("row size = " + size);
for (int i = 0; i < size; i++) {
System.out.println("rows[" + i + "] = " + rows.get(i));
ArrayList cells = (ArrayList)(rows.get(i));
System.out.println("cell size = " + cells.size());
for(int j = 0; j < cells.size(); j ++) {
System.out.println("cells[" + j + "] = " + cells.get(j));
}
}
}
}
"
p,"
q"这样有多余的"号
import java.util.*;
import java.io.*;public class CSVParser { private StringBuffer buf = null; public ArrayList parse() {
String str;
ArrayList rows = new ArrayList();
Pattern pRows = Pattern.compile("(([^\n\"]*(\"[^\"]*(\"{2})*[^\"]*\")*)[^\n]*)\n");
//Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\",)|(([^,]*),)");
Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
Matcher mRows = pRows.matcher(buf);
//System.out.println("-----------rows------------------");
while(mRows.find()) {
//System.out.println("{" + mRows.group(1) + "}");
Matcher mCells = pCells.matcher(mRows.group(1) + ",");
ArrayList cells = new ArrayList();
while(mCells.find()) {
str = mCells.group();
//System.out.println("[" + str + "]");
//str = str.replaceAll("(?sm)(\"(\"))|(\"([^\"])?)|(^\\x0a(.*))", "$2");
//str = str.replaceAll("(?sm)(^[\\x0a]?([^,]*)[,\\x0a]?$)", "$2");
str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
rows.add(cells);
}
return rows;
} public void readCSVFile(File CSVFile) {
int chr;
buf = new StringBuffer("");
try {
BufferedReader in = new BufferedReader(new FileReader(CSVFile));
while((chr=in.read()) != -1) {
buf.append((char)chr);
//System.out.print((char)chr);
}
in.close();
System.out.println("-------------csv file content--------------");
System.out.print(buf);
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
} public static void main(String[] args) throws Throwable {
File file = new File("Book3.csv");
CSVParser parser = new CSVParser();
parser.readCSVFile(file);
ArrayList rows = parser.parse();
int size = rows.size();
System.out.println("\n\n--------------parsed result------------------");
System.out.println("row size = " + size);
for (int i = 0; i < size; i++) {
System.out.println("rows[" + i + "] = " + rows.get(i));
ArrayList cells = (ArrayList)(rows.get(i));
System.out.println("cell size = " + cells.size());
for(int j = 0; j < cells.size(); j ++) {
System.out.println("cells[" + j + "] = " + cells.get(j));
}
}
}
}继续来测试吧
但是不明白这一句
str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
有什么好的解析.csv文件的方法吗?
请给个例子