0

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

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,减持
168,70,24.8,正常
181,89,27.2,增持
178,100,31.6,肥胖
4

5 回答 5

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

编辑:

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


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

美国广播公司,123

rtf,434

lmo,554


然后您的字符串数组列表将如下所示:

列表项 1: String[] {"abc", "123"}

列表项 2:String[] {"rtf", "434"}

列表项 3: String[] {"lmo", "554"}

于 2013-09-12T15:36:42.287 回答
1

records声明如下:

List<String[]> records ...

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

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


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

records.add(fields);

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

于 2013-09-12T15:41:03.693 回答
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 }
于 2013-09-12T15:44:44.100 回答
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
}

分裂()

于 2013-09-12T15:49:07.533 回答
0

该方法使用列表。

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

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

于 2013-09-12T15:41:14.853 回答