使用方法阅读大型 txt 文件 JAVA

Read BIG txt file with metod JAVA

您好,我在读取文件时遇到问题。

我有一个很大的 .txt 文件 (500MB)

我想用方法读取行我开始方法 medhor rsault 是第一行。 我开始方法第二和 return 第二行

我有这个代码。我保存最后读取的行并读取 line+1 但程序在每一行停止 < 最后读取行。如果我读到 100 000< 行,它就太长了。

public  static Boolean Jedno(){
        int poradievtahu=0;
        int[] tah=new int[7];
        String subor= "C:/Users/Paradox/workspace/Citaj_po_Riadku/all6.txt";
        Scanner sc;
        try {
            sc = new Scanner(new File(subor));
            int lineIndex = 0;
    cit:    while(sc.hasNextLine()) {
            String line = sc.nextLine();
            if(lineIndex++ >= pocetC+1) {
                System.out.print("Zvacsujem "+ (pocetC+1) + " " + line);
                // do something
                poradievtahu=-1;
                Scanner scanner=new Scanner(line);
                        while(scanner.hasNextInt()){
                           int pom= scanner.nextInt();

                          tah[++poradievtahu]=pom;
                           if (poradievtahu==5){
                               poradievtahu=-1;
                               pocetC++;

                               if ((pocetC%(55935)==0)&&(pocetC!=0)){
                                  Calendar cal = Calendar.getInstance();
                                 PrintWriter writer4 = new PrintWriter(new BufferedWriter(new FileWriter("nove.txt", true)));
                                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                                    writer4.println("Ďalšia 1/1470  in " + sdf.format(cal.getTime()));
                                    writer4.println(Arrays.toString(tah));
                                    writer4.close();
                               }
                              if (pocetC>=13983816){
                                   //berem=false;                                   
                                   PrintWriter writer4 = new PrintWriter(new BufferedWriter(new FileWriter("mozne.txt", true)));
                                    Calendar cal = Calendar.getInstance();
                                   SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                                    writer4.println("End file in " + sdf.format(cal.getTime()));                                    
                                    writer4.close();    

                                    return true;
                               }

                              Pocty.hladam=tah;
                           }
                        }
                break cit;
            }
        }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

请问您有什么解决问题的方法吗?但是如果我设置第 500 000 行,它会超过 1 秒。但是文件有 19 000 000 行..

我不确定我是否理解你的想法,但如果你想处理文件中从 X 行到 Y 行的某些行,我建议使用 File.lines() 方法:

public static void processLinesFromPoint(int startLine, int numberOfLinesToProcess) throws IOException {
    //assume startLine = 5000
    //       numberOfLinesToProcess = 500
    Stream<String> lines = Files.lines(Paths.get(pathToYourFile)).skip(startLine).limit(numberOfLinesToProcess);
    //lines.forEach method to loop through lines 5000 to 5500  (startLine+numberOfLinesToProcess)
    //and printing each line
    lines.forEach(currentLine->{
        //here goes your logic to process each line...
        System.out.println(currentLine)
    });
}

Files.lines 具有函数,因此您可以获得所需的行数并使用 Files.lines().count() 来获取文件中的总行数。

P.S:我用这个方法处理2Gb以上的文件,希望回答有用)