本人博客
http://blog.sina.com.cn/u/1256662733 package com.csv.util;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class CsvUtil {
private String filename = null; private BufferedReader bufferedreader = null; private List list = new ArrayList(); public CsvUtil() { } public CsvUtil(String filename) throws IOException {
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
list.add(stemp);
}
} public List getList() throws IOException {
return list;
} public int getRowNum() {
return list.size();
} public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
} public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
} public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str=new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
} public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
temp = list.get(row).toString().split(",")[col];
} else if (colnum == 1) {
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}
public void CsvClose() throws IOException {
this.bufferedreader.close();
} public void test() throws IOException {
CsvUtil cu = new CsvUtil("G:/Book2.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();) {
System.out.println(itt.next().toString());
}
System.out.println(cu.getRowNum());
System.out.println(cu.getColNum());
System.out.println(cu.getRow(0));
System.out.println(cu.getCol(0));
System.out.println(cu.getString(0, 0));
cu.CsvClose();
} public static void main(String[] args) throws IOException {
CsvUtil test = new CsvUtil();
test.test();
}
}
http://blog.sina.com.cn/u/1256662733 package com.csv.util;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class CsvUtil {
private String filename = null; private BufferedReader bufferedreader = null; private List list = new ArrayList(); public CsvUtil() { } public CsvUtil(String filename) throws IOException {
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
list.add(stemp);
}
} public List getList() throws IOException {
return list;
} public int getRowNum() {
return list.size();
} public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
} public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
} public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str=new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
} public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
temp = list.get(row).toString().split(",")[col];
} else if (colnum == 1) {
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}
public void CsvClose() throws IOException {
this.bufferedreader.close();
} public void test() throws IOException {
CsvUtil cu = new CsvUtil("G:/Book2.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();) {
System.out.println(itt.next().toString());
}
System.out.println(cu.getRowNum());
System.out.println(cu.getColNum());
System.out.println(cu.getRow(0));
System.out.println(cu.getCol(0));
System.out.println(cu.getString(0, 0));
cu.CsvClose();
} public static void main(String[] args) throws IOException {
CsvUtil test = new CsvUtil();
test.test();
}
}
解决方案 »
- 一个try/catch问题
- 二分查找
- 关于JSTL的问题
- 为什么告诉我找不到Thread类
- eclipse 我创建jsp文件 时为什么老是提示 the import java.util is never used
- 如何截获通过JDBC发往后台的SQL语句,并且在修改之后再如ORACLE数据库
- 关于socket的问题,
- JCreator下运行程序提示如下错误该如何解决呢?
- 如何使用javamail包?
- java里面有类似VC++的Class wizzard 吗?每个方法都要自己手工加太麻烦了吧?
- applet不从网上加载而从本地加载的问题, 高手请解释
- Integer i = new Integer("0x10");封装器问题
"abc, xyz", "123", "A""B""C"应该被解析成
abc, xyz
123
A"B"C
"abc, xyz", "123", "A""B""C"应该被解析成
abc, xyz
123
A"B"C
-----------------------------------
拜托你测试过以后再发表评论
本人测试不存在这个问题
如果你不了解什么是csv格式 也不要乱发表意见
在excel中显示为四个单元格
自己试下 你不是是在怀疑 微软的csv解析也有问题
B1: 123
C1: A"B"C懂了没有!!!
"A1: abc, xyz",B1: 123,"C1: A""B""C"
自己测试下嘛
1
3
A1: abc, xyz,B1: 123,C1: A"B"C
A1: abc, xyz
A1: abc, xyz
而不是
"A1: abc, xyz",B1: 123,"C1: A""B""C"
1
4
"A1: abc, xyz",B1: 123,"C1: A""B""C"
"A1: abc
"A1: abc
吗???!!!为什么这个文件
"A1: abc, xyz",B1: 123,"C1: A""B""C"
用excel打开,看到的是
A1: abc, xyz B1: 123 C1: A"B"C
共3个单元格,而你这里是4个?
难道是微软出了个BUG,那么多年,那么多人都没有发现,就大侠你的程序一下子就测出这个BUG来了?!
荒谬!!!
我测过了
贴一段Csv来凑热闹,鬼子的东西import java.io.*;
import java.util.*;
public class CsvReader extends BufferedReader { protected String delimiter = ","; protected char escape = '\"'; protected String nowLine = null;
public CsvReader(Reader in) {
super(in);
}
public CsvReader(Reader in, String d) {
this(in);
delimiter = d;
}
public CsvReader(Reader in, char d) {
this(in, String.valueOf(d));
}
public CsvReader(Reader in, int sz) {
super(in, sz);
}
public CsvReader(Reader in, int sz, String d) {
this(in, sz);
delimiter = d;
}
public CsvReader(Reader in, int sz, char d) {
this(in, sz, String.valueOf(d));
}
public String getDelimiter() {
return delimiter;
}
public String readLine() throws IOException {
nowLine = super.readLine();
return nowLine;
}
public String[] readLineAsArray() throws IOException {
List v = readLineAsList();
if (v == null)
return null; String items[] = new String[v.size()];
for (int i = 0; i < v.size(); i++) {
items[i] = (String)v.get(i);
} return items;
}
public List readLineAsList() throws IOException {
String line = readLine();
if (line == null)
return null; return getCsvItems(line);
}
public String getNowLine() {
return nowLine;
}
protected List getCsvItems(String line) {
List v = new ArrayList(); int startIdx = 0;
int searchIdx = -1; StringBuffer sbLine = new StringBuffer(line); while ((searchIdx = sbLine.toString().indexOf(delimiter, startIdx))
!= -1) {
String buf = null;
if (sbLine.charAt(startIdx) != escape) {
buf = sbLine.substring(startIdx, searchIdx);
startIdx = searchIdx + 1;
} else {
int escapeIdx = -1;
searchIdx = startIdx;
boolean findDelimiter = false; while ((escapeIdx =
sbLine.toString().indexOf(escape, searchIdx + 1))
!= -1
&& sbLine.length() > escapeIdx + 1) {
char nextChar = sbLine.charAt(escapeIdx + 1);
if (delimiter.indexOf(nextChar) != -1) {
buf = sbLine.substring(startIdx + 1, escapeIdx);
startIdx = escapeIdx + 2; findDelimiter = true;
break;
}
if (nextChar == escape) {
sbLine.deleteCharAt(escapeIdx);
escapeIdx--;
}
searchIdx = escapeIdx + 1;
}
if (!findDelimiter) {
break;
}
}
v.add(buf);
} if (startIdx < sbLine.length()) {
int lastIdx = sbLine.length() - 1;
if (sbLine.charAt(startIdx) == escape
&& sbLine.charAt(lastIdx) == escape) {
sbLine.deleteCharAt(lastIdx);
sbLine.deleteCharAt(startIdx);
}
v.add(sbLine.substring(startIdx, sbLine.length()));
} else if (startIdx == sbLine.length()) {
v.add("");
} return v;
}
}