需要了解这个java代码的工作原理

时间:2013-09-12 15:33:49

标签: java

到目前为止我所理解的是,它将逐行读取文件,同时,它将拆分记录并将其保存在字段数组中,之后我无法理解它将添加的内容记录列表....因为它直接将fields数组添加到列表中。

String fileName = "C:\\data.csv"

public static Collection<String[]> getTestData(fileName) throws IOException {
    List<String[]> records = new ArrayList<String[]>();
    String record;
    BufferedReader file = new BufferedReader(new FileReader(fileName));
    while ((record=file.readLine())!=null) {
        String fields[] = record.split(",");
        records.add(fields);
    }
    file.close();
    return records;
}

CSV文件

160,45,17.6,Underweight
168,70,24.8,Normal
181,89,27.2,Overweight
178,100,31.6,Obesity

5 个答案:

答案 0 :(得分:1)

  1. 设置字符串数组列表。
  2. 从data.csv
  3. 读取每一行
  4. 对于每一行,使用逗号作为分隔符拆分为数组,并将数组添加到列表中
  5. 返回列表。
  6. 编辑:

    你最终得到的是一个字符串数组列表。


    如果您的csv文件如下所示:

    ABC,123

    RTF,434

    LMO,554


    然后你的字符串arrys列表如下所示:

    list item 1:String [] {“abc”,“123”}

    list item 2:String [] {“rtf”,“434”}

    list item 3:String [] {“lmo”,“554”}

答案 1 :(得分:1)

使用以下内容声明

records

List<String[]> records ...

这意味着它是一个字符串数组列表。

列表中的每个项目都将具有String[]

类型

fields是一个数组,所以当执行folloiwng行时

records.add(fields);

它将fields数组添加到列表中。

答案 2 :(得分:1)

String fileName = "C:\\data.csv" //set the location of a comma seperated value file

public static Collection<String[]> getTestData(fileName) throws IOException {
    List<String[]> records = new ArrayList<String[]>(); // instantiate an array list object to hold the fields to be returned
    String record;
    BufferedReader file = new BufferedReader(new FileReader(fileName)); // open the file for reading
    while ((record=file.readLine())!=null) { //read the file line by line, execute the code in the curly brackets until an empty line or end of file is reached
        String fields[] = record.split(","); //split up each line into an array, each elemet will be delimited by a comma
        records.add(fields); // add each element to the array list object created earlier
    }
    file.close();
    return records; //return all the fields found in the file
}

例如,文件包含:

one,two,three
red,green,gold

此方法将返回一个字符串数组列表:

{ "one","two","three" }
{ "red","green","gold" }

修改:

对于您添加的数据,这将返回一个数组列表:

{ 160, 45, 17.6, Underweight }
{ 168, 70, 24.8, Normal }
{ 181, 89, 27.2, Overweight }
{ 178, 100, 31.6, Obesity }

答案 3 :(得分:0)

该方法使用List。

这很像一个数组数组,除了外部数组是动态的。

String#split返回一个字符串数组。我们将该数组添加到列表中,并在最后返回数组列表。

答案 4 :(得分:0)

String fileName = "C:\\data.csv"       // This is the file to be read

// Method is public: can be called from anywhere in the package and class
//           static so can be called without instance.
//           return type is Collection<String []> Collection Of String Array
//           takes filename as argument
//           throws IOException since we are reading from a file

public static Collection<String[]> getTestData(fileName) throws IOException {

    // record is the List which is a Collection of String Arrays we will return
    List<String[]> records = new ArrayList<String[]>();

    String record;

    // Reading the File
    BufferedReader file = new BufferedReader(new FileReader(fileName));

    // Read line by line unless the last line is null
    while ((record=file.readLine())!=null) {
        String fields[] = record.split(",");   // splits the line with delimiter ","
        records.add(fields);
    }
    file.close();   // Close the file
    return records; // return the Collection
}

split()