How can I read a large text file line by line using Java?

前端 未结 21 2392
心在旅途
心在旅途 2020-11-21 05:48

I need to read a large text file of around 5-6 GB line by line using Java.

How can I do this quickly?

相关标签:
21条回答
  • 2020-11-21 06:15

    You can use this code:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadTextFile {
    
        public static void main(String[] args) throws IOException {
    
            try {
    
                File f = new File("src/com/data.txt");
    
                BufferedReader b = new BufferedReader(new FileReader(f));
    
                String readLine = "";
    
                System.out.println("Reading file using Buffered Reader");
    
                while ((readLine = b.readLine()) != null) {
                    System.out.println(readLine);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 06:18

    A common pattern is to use

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = br.readLine()) != null) {
           // process the line.
        }
    }
    

    You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.

    EDIT: A less common pattern to use which avoids the scope of line leaking.

    try(BufferedReader br = new BufferedReader(new FileReader(file))) {
        for(String line; (line = br.readLine()) != null; ) {
            // process the line.
        }
        // line is not visible here.
    }
    

    UPDATE: In Java 8 you can do

    try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
            stream.forEach(System.out::println);
    }
    

    NOTE: You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until GC does it much later.

    0 讨论(0)
  • 2020-11-21 06:19

    FileReader won't let you specify the encoding, use InputStreamReaderinstead if you need to specify it:

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));         
    
        String line;
        while ((line = br.readLine()) != null) {
            // process the line.
        }
        br.close();
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    If you imported this file from Windows, it might have ANSI encoding (Cp1252), so you have to specify the encoding.

    0 讨论(0)
  • 2020-11-21 06:19

    You can use streams to do it more precisely:

    Files.lines(Paths.get("input.txt")).forEach(s -> stringBuffer.append(s);
    
    0 讨论(0)
  • 2020-11-21 06:20

    What you can do is scan the entire text using Scanner and go through the text line by line. Of course you should import the following:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public static void readText throws FileNotFoundException {
        Scanner scan = new Scanner(new File("samplefilename.txt"));
        while(scan.hasNextLine()){
            String line = scan.nextLine();
            //Here you can manipulate the string the way you want
        }
    }
    

    Scanner basically scans all the text. The while loop is used to traverse through the entire text.

    The .hasNextLine() function is a boolean that returns true if there are still more lines in the text. The .nextLine() function gives you an entire line as a String which you can then use the way you want. Try System.out.println(line) to print the text.

    Side Note: .txt is the file type text.

    0 讨论(0)
  • 2020-11-21 06:20
    BufferedReader br;
    FileInputStream fin;
    try {
        fin = new FileInputStream(fileName);
        br = new BufferedReader(new InputStreamReader(fin));
    
        /*Path pathToFile = Paths.get(fileName);
        br = Files.newBufferedReader(pathToFile,StandardCharsets.US_ASCII);*/
    
        String line = br.readLine();
        while (line != null) {
            String[] attributes = line.split(",");
            Movie movie = createMovie(attributes);
            movies.add(movie);
            line = br.readLine();
        }
        fin.close();
        br.close();
    } catch (FileNotFoundException e) {
        System.out.println("Your Message");
    } catch (IOException e) {
        System.out.println("Your Message");
    }
    

    It works for me. Hope It will help you too.

    0 讨论(0)
提交回复
热议问题