简体   繁体   English

JAVA - 将CSV导入ArrayList

[英]JAVA - import CSV to ArrayList

I'm trying import CSV file to Arraylist using StringTokenizer : 我正在尝试使用StringTokenizer导入CSV文件到Arraylist

public class Test
{
  public static void main(String [] args)
  {
    List<ImportedXls> datalist = new ArrayList<ImportedXls>();

    try
    {
      FileReader fr = new FileReader("c:\\temp.csv");
      BufferedReader br = new BufferedReader(fr);
      String stringRead = br.readLine();

      while( stringRead != null )
      {
        StringTokenizer st = new StringTokenizer(stringRead, ",");
        String docNumber = st.nextToken( );
        String note = st.nextToken( );  /** PROBLEM */
        String index = st.nextToken( ); /** PROBLEM */

        ImportedXls temp = new ImportedXls(docNumber, note, index);
        datalist.add(temp);

        // read the next line
        stringRead = br.readLine();
      }
      br.close( );
    }
    catch(IOException ioe){...}

    for (ImportedXls item : datalist) {
      System.out.println(item.getDocNumber());
    }
  }
}

I don't understand how the nextToken works, because if I keep the initialize three variables ( docNumber , note and index ) as nextToken() , it fails on: 我不明白nextToken是如何工作的,因为如果我将初始化三个变量( docNumbernoteindex )保持为nextToken() ,它将失败:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at _test.Test.main(Test.java:32)

If I keep docNumber only, it works. 如果我只保留docNumber,它就可以了。 Could you help me? 你可以帮帮我吗?

It seems that some of the rows of your input file have less then 3 comma separated fields.You should always check if tokenizer has more tokens (StringTokenizer.hasMoreTokens), unless you are are 100% sure your input is correct. 您的输入文件的某些行似乎有少于3个以逗号分隔的字段。您应该始终检查tokenizer是否有更多令牌(StringTokenizer.hasMoreTokens),除非您100%确定您的输入是正确的。

CORRECT parsing of CSV files is not so trivial task. CORRECT解析CSV文件并非如此简单。 Why not to use a library that can do it very well - http://opencsv.sourceforge.net/ ? 为什么不使用能够做得很好的库 - http://opencsv.sourceforge.net/

Seems like your code is getting to a line that the Tokenizer is only breaking up into 1 part instead of 3. Is it possible to have lines with missing data? 好像你的代码到达了一条线,Tokenizer只分成1部分而不是3部分。是否可能有缺少数据的行? If so, you need to handle this. 如果是这样,你需要处理这个问题。

Most probably your input file doesn't contain another element delimited by , in at least one line. 最有可能你的输入文件不包含由分隔的另一种元素,至少在一行。 Please show us your input - if possible the line that fails. 请告诉我们您的输入 - 如果可能,请输入失败的行。

However, you don't need to use StringTokenizer . 但是,您不需要使用StringTokenizer Using String#split() might be easier: 使用String#split()可能更容易:

...
while( stringRead != null )
{
    String[] elements = stringRead.split(",");

    if(elements.length < 3) {
      throw new RuntimeException("line too short"); //handle missing entries
    }

    String docNumber = elements[0];
    String note = elements[1];
    String index = elements[2];

    ImportedXls temp = new ImportedXls(docNumber, note, index);
    datalist.add(temp);

    // read the next line
    stringRead = br.readLine();
}
...

You should be able to check your tokens using the hasMoreTokens() method. 您应该能够使用hasMoreTokens()方法检查您的令牌。 If this returns false, then it's possible that the line you've read does not contain anything (ie, an empty string). 如果返回false,则您读取的行可能不包含任何内容(即空字符串)。

It would be better though to use the String.split() method--if I'm not mistaken, there were plans to deprecate the StringTokenizer class. 尽管使用String.split()方法会更好 - 如果我没有弄错,有计划弃用StringTokenizer类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM