首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用metod JAVA读取大txt文件

用metod JAVA读取大txt文件
EN

Stack Overflow用户
提问于 2017-02-16 21:40:31
回答 1查看 269关注 0票数 0

您好,我有一个读取文件的问题。

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

我想读一行方法,第一行是medhor方法。我启动方法second并返回第二行

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

代码语言:javascript
复制
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行,就会超过1S,但是文件有19,000,000行..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-16 22:09:42

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

代码语言:javascript
复制
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()来获取文件中的总行数。

附言:我使用这个方法来处理超过2 2Gb的文件,我希望答案会有用。)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42275602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档