Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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
用metod JAVA读取大txt文件_Java_Text Files_Readfile - Fatal编程技术网

用metod JAVA读取大txt文件

用metod JAVA读取大txt文件,java,text-files,readfile,Java,Text Files,Readfile,您好,我在读取文件时遇到问题 我有一个大的.txt文件(500MB) 我想用我开始的方法读一行,方法是翻腾第一行。 我启动第二个方法并返回第二行 我有这个密码。我保存最后读取行和读取行+1,但程序停止在每一行{ //这是你处理每一行的逻辑。。。 系统输出打印项次(当前行) }); } Files.lines具有函数,因此您可以获取所需的行数,并使用Files.lines().count()获取文件中的总行数 注:我使用此方法处理2Gb以上的文件,希望答案会有用)我不确定是否了解您的想法,但如果

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

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

我想用我开始的方法读一行,方法是翻腾第一行。 我启动第二个方法并返回第二行

我有这个密码。我保存最后读取行和读取行+1,但程序停止在每一行<最后读取行。如果我读10万行,那就太长了

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;
    }

请问您有一些IDE如何解决问题?但如果我设为500000行,则超过1s。但是文件有1900000行

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

public static void processlines frompoint(int stackline,int numberOfLinesToProcess)引发IOException{
//假设STARTINE=5000
//numberOfLinesToProcess=500
streamlines=Files.lines(path.get(pathToYourFile)).skip(startLine).limit(numberOfLinesToProcess);
//lines.forEach方法在5000到5500行之间循环(startLine+numberOfLinesToProcess)
//并打印每一行
lines.forEach(当前行->{
//这是你处理每一行的逻辑。。。
系统输出打印项次(当前行)
});
}
Files.lines具有函数,因此您可以获取所需的行数,并使用Files.lines().count()获取文件中的总行数


注:我使用此方法处理2Gb以上的文件,希望答案会有用)

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

public static void processlines frompoint(int stackline,int numberOfLinesToProcess)引发IOException{
//假设STARTINE=5000
//numberOfLinesToProcess=500
streamlines=Files.lines(path.get(pathToYourFile)).skip(startLine).limit(numberOfLinesToProcess);
//lines.forEach方法在5000到5500行之间循环(startLine+numberOfLinesToProcess)
//并打印每一行
lines.forEach(当前行->{
//这是你处理每一行的逻辑。。。
系统输出打印项次(当前行)
});
}
Files.lines具有函数,因此您可以获取所需的行数,并使用Files.lines().count()获取文件中的总行数


附言:我使用这种方法处理2Gb以上的文件,我希望答案会有用)

以及如何将startline转换为string?正如您所看到的,lines变量是Stream数据类型,因此您可以使用lambda(Java8)循环流<代码>行.forEach(l->{//这里是处理每行的逻辑…})l是STARTINE,它已经是字符串了loop@user7575308更新了答案,希望现在更清楚。以及如何将startline转换为string?正如您所看到的,lines变量是Stream数据类型,所以您可以使用lambda(Java8)循环流<代码>行.forEach(l->{//这里是处理每行的逻辑…})l是STARTINE,它已经是字符串了loop@user7575308更新了答案,希望现在更清楚。
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)
    });
}