本人博客
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();
}
}

解决方案 »

  1.   

    有严重到极点的bug!比如文件某行内容:
    "abc, xyz", "123", "A""B""C"应该被解析成
    abc, xyz
    123
    A"B"C
      

  2.   

    有严重到极点的bug!比如文件某行内容:
    "abc, xyz", "123", "A""B""C"应该被解析成
    abc, xyz
    123
    A"B"C
    -----------------------------------
    拜托你测试过以后再发表评论  
    本人测试不存在这个问题
      

  3.   

    补充一下
    如果你不了解什么是csv格式 也不要乱发表意见
      

  4.   

    跟你说了,你错了,还不信!!!!!!拜托,“如果你不了解什么是csv格式 也不要乱发表意见”这句话请你自己先了解一下CSV的格式!!!我还没说那种一条数据跨多行的CSV(这种跨行的,可以用Excel保存为CSV格式得到)你以为CSV就是看到“,”就是两个字段了???!!!
      

  5.   

    我晕  拜托你用excel保存试试  看看"a""b"是几行
      

  6.   

    "abc, xyz", "123", "A""B""C"
    在excel中显示为四个单元格
    自己试下  你不是是在怀疑 微软的csv解析也有问题
      

  7.   

    在excel里3个单元格里分别保存A1: abc, xyz
    B1: 123
    C1: A"B"C懂了没有!!!
      

  8.   

    此外,麻烦你自己google一下csv format或者CSV格式之类。
      

  9.   

    打开以后
    "A1: abc, xyz",B1: 123,"C1: A""B""C"
    自己测试下嘛
      

  10.   

    就这个文件,你能测试出"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来了?!
    荒谬!!!
      

  11.   

    虽然我态度一向不怎么好,但是从来不在帖子里骂人,但是今天不得不骂lz真是真眼瞎啊!!!你在Excel里,写了3个字段,2个双引号,用记事本打开,却有3个逗号(而不是“三刀两段”),8个引号,这些差异,外加我无数次的提示,都不能引起你的注意,不是瞎子是什么?再说,我从没要求你认错什么的,只是想让你,特别是那些路过的人知道,你的程序有严重BUG,根本无法识别大部分CSV文件,希望你还有其他想偷懒的人,不要用这段代码作学习以外的任何事情,这也就是为什么我到你blog里面留言的原因。
      

  12.   

    严重同意有错!!
    我测过了
    贴一段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;
    }
    }