简体   繁体   English

Java中的缓冲读取器,行数,拆分,解析

[英]Buffered Reader, Line count, Split, Parse in Java

Actually, I am assigned a task where I have a xyz.txt/CSV file which will basically have numeric values and I am supposed to pass it through BUFFERED READER then split those values and finally parse them. 实际上,我分配了一个任务,其中有一个xyz.txt / CSV文件,该文件将基本上具有数字值,并且应该将其传递给BUFFERED READER,然后拆分这些值并最终对其进行解析。

So I have a Java code can body help me with it. 所以我有一个Java代码可以帮助我。

package javaapplication12;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class JavaApplication12 {   

    public static void main(String[] args) {

        String count= "F:\\Gephi\\number.txt";            
        BufferedReader br = null;
        FileReader fr = null;

        try {           
            fr = new FileReader(count);
            br = new BufferedReader                       

            // AT THIS POINT THERE SHOULD BE SOME THING THAT COUNTS NUMBER OF LINES USING COUNT++ OR SOMETHING LIKE THIS//    
            String sCurrentLine;    
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }                            
        }             
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
                try {
                    if (br != null)
                       br.close();

                    if (fr != null)
                       fr.close();
                } 
                catch (IOException ex) {
                    ex.printStackTrace();
                }
        }            
     }
}
                // COMING TO THIS POINT THE ABOVE VALUES OF .TXT FILE SHOULD BE SPLIT USING SPLIT PARAMETER//
               // AFTER SPLITTING THE SPLIT VALUE SHOULD BE KEPT IN AN ARRAY AND THEN EVENTUALLY PARSED//

Or IF Anybody can rewrite the code in another way of the above-stated problem, it shall also be appreciated. 或者,如果任何人都可以用上述问题的另一种方式重写代码,也将不胜感激。

Here is my solution with Java 8: 这是我的Java 8解决方案:

 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.List;
 import java.util.stream.Collectors;

 public class BR {

 public static void main(String[] args) {
    String fileName = "br.txt";
    //for the csv format
    String regex = ", ";
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        List<String[]> lines = br.lines()
                .map(line -> line.split(regex))
                .collect(Collectors.toList());

        parse(lines);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static void parse(List<String[]> lines) {
    //Do your stuff here
}

 }

The initialization of the BufferedReader is in the try block (this approach is called try with resources), since BufferedReader implements AutoCloseable (an interface), so in case an exception is thrown the reader will close. BufferedReader的初始化在try块中(此方法称为带资源尝试),因为BufferedReader实现了AutoCloseable(一个接口),因此如果引发异常,则阅读器将关闭。

The br.lines() method returns all lines from the file. br.lines()方法返回文件中的所有行。 In the map function you are passing a line that is rad in a lambda. 在map函数中,您传递的是lambda中的rad线。 The line is split using the split variable (for CSV format it is ', ') and is returned and collected. 该行使用split变量进行拆分(对于CSV格式为','),并将其返回并收集。

The result is a List of arrays of String which can be changed in the body of the map function. 结果是一个String数组列表,可以在map函数的主体中对其进行更改。

For more clarification I suggest you check some Java 8 tutorials and you will fully understand what is going on. 为了得到更多的澄清,我建议您检查一些Java 8教程,您将完全了解发生了什么。

This solution might not be appropriate for your knowledge level (I guess), but hope it inspires you to check some fancier and modern approaches. 该解决方案可能不适合您的知识水平(我想),但希望它能激发您检查一些更现代的方法。

Have a nice day. 祝你今天愉快。

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

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