Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java读取文件时如何处理新行_Java_Arrays_Csv - Fatal编程技术网

使用Java读取文件时如何处理新行

使用Java读取文件时如何处理新行,java,arrays,csv,Java,Arrays,Csv,我试图读取一个巨大的文件,其中新行没有空格、逗号、新行字符或任何东西表示 示例:line1element1、line1element2、line1element3、line2element1、line2element2,依此类推 该文件是一个csv文件,我的阅读方式如下: public static void main(String[] args) throws Exception { ArrayList<String> list = new ArrayList<>

我试图读取一个巨大的文件,其中新行没有空格、逗号、新行字符或任何东西表示

示例:line1element1、line1element2、line1element3、line2element1、line2element2,依此类推

该文件是一个csv文件,我的阅读方式如下:

public static void main(String[] args) throws Exception {
    ArrayList<String> list = new ArrayList<>();
    String element;
    String filename = "E:\\csv.csv";
    Scanner scanner = new Scanner(new File(filename));
    scanner.useDelimiter(",");

    for (int i = 0; i < 50; i++) {
        element = scanner.next();
        list.add(element);

    }
    System.out.print(list);
}

这会导致问题,因为一行中的element50与element51合并,尽管它应该是新行。

使用BufferedReader,而不是Scanner

File f= ...;
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.nextLine()) != null) {
  String[] columns = line.split(",");
}

使用缓冲读取器,而不是扫描仪

File f= ...;
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.nextLine()) != null) {
  String[] columns = line.split(",");
}
为此使用BufferedReader:

String filename = "E:\\csv.csv";
BufferedReader fileReader = null;

//Delimiter used in CSV file
final String DELIMITER = ",";

String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(filename ));

//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
    //Get all tokens available in line
    String[] tokens = line.split(DELIMITER);
    for(String token : tokens)
    {
        //Print all tokens
        System.out.println(token);
    }
}
为此使用BufferedReader:

String filename = "E:\\csv.csv";
BufferedReader fileReader = null;

//Delimiter used in CSV file
final String DELIMITER = ",";

String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(filename ));

//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
    //Get all tokens available in line
    String[] tokens = line.split(DELIMITER);
    for(String token : tokens)
    {
        //Print all tokens
        System.out.println(token);
    }
}
为什么不使用Apache Commons或OpenCSV中的CSVParser

示例如下:

如果您坚持手动执行此操作,请使用其他注释中提到的BufferedReader。

为什么不使用Apache Commons或OpenCSV中的CSVParser

示例如下:


如果您坚持手动执行此操作,请使用其他注释中提到的BufferedReader。

根据您的描述,您的文件似乎没有每列的标题。用于为您执行此操作-它比Commons CSV和OpenCSV快3倍,并具有丰富的功能

// you have many configuration options here - check the tutorial. By default values are trimmed and blank lines skipped.
CsvParserSettings settings = new CsvParserSettings();

CsvParser parser = new CsvParser(settings);

List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/your/file.csv")));

披露:我是这个图书馆的作者。它是开源的免费Apache V2.0许可证。

从您的描述来看,您的文件似乎没有每列的标题。用于为您执行此操作-它比Commons CSV和OpenCSV快3倍,并具有丰富的功能

// you have many configuration options here - check the tutorial. By default values are trimmed and blank lines skipped.
CsvParserSettings settings = new CsvParserSettings();

CsvParser parser = new CsvParser(settings);

List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/your/file.csv")));

披露:我是这个图书馆的作者。这是一个开源的免费Apache V2.0许可证。

我不需要一个陷阱吗?而且这只会将数据作为一个大列输出,而不是识别多行。这是一个可怕的解决方案,CSV解析有许多复杂之处。对于初学者来说,如果任何值带有换行符,readLine将不起作用。String.split将不正确地处理包含逗号字符的带引号的值。使用CSV解析器,而不是创建自己的解析器。忘记提及:这也是非常缓慢的。试着在一个大文件上运行这段代码,然后自己看看。我不需要一个捕获吗?而且这只会将数据作为一个大列输出,而不是识别多行。这是一个可怕的解决方案,CSV解析有许多复杂之处。对于初学者来说,如果任何值带有换行符,readLine将不起作用。String.split将不正确地处理包含逗号字符的带引号的值。使用CSV解析器,而不是创建自己的解析器。忘记提及:这也是非常缓慢的。试着在一个大文件上运行这段代码,自己看看。