调用在迭代中创建的数组

Calling An Array That Was Created in An Iteration

所以我正在编写一个程序,它将读取一个 csv 文件并将文件的每一行放入一个数组中。我想知道是否可以命名在 while 循环中创建的单数数组。我也很想知道您是否对我如何能够按 csv 文件的列分隔行(包含 csv 文件的行)有任何想法。

这是我的代码:

package sample.package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SampleClass {

/**
 * @param args
 */
public static String fileLocation;                                                  //Used to declare the file path

public static void readAndArray(String fileLocation) throws IOException {           //Method to read a file and put each line into an array
    BufferedReader lineRead = new BufferedReader(new FileReader(fileLocation));     //Read the file and be able to parse it into separate lines

        String line = lineRead.readLine();                                          //Put line into variable so it isn't a boolean statement

        while ((line = lineRead.readLine()) !=null) {                               //Make it possible to print out array as BufferedReader is used

            String[] oneLine = new String[] {line};                                 //Put parsed line in new array and parsing that data for individual spots in array
            System.out.println(oneLine[0]);                                         //Print out each oneLine array
        }
        lineRead.close();                                                           //Neatly close BufferedReader and FileReader
}


public static void main(String[] args) throws IOException{

    readAndArray("filePath"); //Initialize method by inputting the file path


    }
}

非常感谢你们!

首先:欢迎使用 Whosebug!

我假设您的问题与某种教育编程任务有关,如果不是的话:有许多处理 CSV 文件的库(具有其他功能,例如读取 header 行和通过 header/column姓名等)。

但是...为什么编写 CSV 解析器是一项复杂的任务,我的意思是,它基本上只是用逗号分隔的值,phhh!?

长话短说:有 RFC 4180, but don't expect that all your .csv files stick to it. Quoting Wikipedia:

The CSV file format is not fully standardized. The basic idea of separating fields with a comma is clear, but that idea gets complicated when the field data may also contain commas or even embedded line-breaks. CSV implementations may not handle such field data, or they may use quotation marks to surround the field. Quotation does not solve everything: some fields may need embedded quotation marks, so a CSV implementation may include escape characters or escape sequences.

在您意识到并希望理解所提供的有关兼容性的警告之后,以下代码示例完全忽略了它并提供了一个快速而肮脏的解决方案(不要在生产中使用它,认真的......如何不被解雇101:使用经过良好测试的库,例如 opencsv or Apache Commons CSV):

public static void main(String[] args)
{
    Path path = Paths.get("some path"); // TODO Change to path to an CSV file

    try (Stream<String> lines = Files.lines(path))
    {
        List<List<String>> rows = lines
                // Map each line of the file to its fields (String[])
                .map(line -> line.split(","))
                // Map the fields of each line to unmodifiable lists
                .map(fields -> Collections.unmodifiableList(Arrays.asList(fields))
                // Collect the unmodifiable lists in an unmodiable list (listception)
                .collect(Collectors.toUnmodifiableList());

        // Ensure file is not empty.
        if (rows.isEmpty())
        {
            throw new IllegalStateException("empty file");
        }

        // Ensure all lines have the same number of fields.
        int fieldsPerRow = rows.get(0).size();
        if (!rows.stream().allMatch(row -> row.size() == fieldsPerRow))
        {
            throw new IllegalStateException("not all rows have the same number of fields");
        }

        // Assume the file has a header line appearing as the first line.
        System.out.printf("Column names: %s\n", rows.get(0));

        // Read the data rows.
        rows.stream()
                .skip(1) // Skip header line
                .forEach(System.out::println);
    }
    catch (IOException | UncheckedIOException e)
    {
        e.printStackTrace(); // TODO Handle exception
    }
}

此代码假定:

  • 字段之间用逗号分隔
  • 一行被认为是由换行符 ('\n')、回车符 return ('\r') 或紧随其后的回车符 return 中的任何一个终止的通过换行(Files.lines() 使用的 BufferedReader 的行为)
  • 所有行的字段数相同
  • 第一行是header行
  • 实现者懒得介意用双引号括起来的字段

快速而肮脏...并且违反了 RFC ;)