简体   繁体   English

每'x'行数反转txt文件中的行顺序-JAVA

[英]reversing order of lines in txt file every 'x' number of lines - JAVA

so i have an input txt file where i have to take the first 50 lines and reverse it's order so that the file will start with the 50th line, then the 49th, until the 1st, then continues with the 100th line followed by the 99th, and so on... 所以我有一个输入txt文件,在其中我必须采用前50行并颠倒顺序,以便文件从第50行开始,然后是第49行,直到第1行,然后继续第100行,然后是第99行,等等...

but i can only store at most 50 elements. 但我最多只能存储50个元素。 i can't store more than that. 我不能存储更多。 the code i've written so far only grabs the first 50 lines and reverses them, but i dont know how to make it continue on. 到目前为止,我编写的代码仅抓取前50行并将其反转,但我不知道如何继续进行。

this is what i have so far: 这是我到目前为止所拥有的:

ArrayList<String> al = new ArrayList<String>();
int running = 0;

while(running == 0) {
    for (String line = r.readLine(); line != null; line = r.readLine()) {
        if(al.size() <50 ) {
            al.add(line);
        }
    }
    Collections.reverse(al);
    for (String text : al) {
        w.println(text);
    }

    if(al.size() < 50) {
        break;
    }
    al.clear();
}

idk why my while loop won't keep running, im only getting the first 50 lines reversed in my output file. idk为什么我的while循环无法继续运行,我只能在输出文件中反转前50行。

This: 这个:

    for (String line = r.readLine(); line != null; line = r.readLine()) {
        if(al.size() <50 ) {
            al.add(line);
        }
    }

reads all the lines in the file, stores the first fifty in al , and discards the rest. 读取文件中的所有行,将前五十行存储在al ,然后丢弃其余行。 After you then process the results of al , there's nothing more for your program to do: it's read the whole file. 然后,您处理al的结果之后,您的程序便无所事事:它读取了整个文件。

There's a great blog post on how to debug small programs, that I highly recommend: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ 我强烈建议您撰写一篇很棒的博客文章,介绍如何调试小程序: http : //ericlippert.com/2014/03/05/how-to-debug-small-programs/

And in your specific case, I suggest breaking your program into functions. 在您的特定情况下,我建议将程序分成功能。 One of those functions will be "read up to n lines, and return them in an array-list". 这些功能之一将是“最多读取n行,并将它们返回到数组列表中”。 This sort of structure makes it easier to reason about each part of the program. 这种结构使对程序各部分的推理变得容易。

Your initial loop: 您的初始循环:

for (String line = r.readLine(); line != null; line = r.readLine()) {
    if(al.size() <50 ) {
        al.add(line);
    }
}

Continues to read lines after you've filled al through to the end of the file - it just doesn't put them in the list. 继续读取线填完后, al通过对文件的末尾-它只是没有把它们在列表中。

You most likely need something like: 您最有可能需要以下内容:

for (String line = r.readLine(); line != null; line = r.readLine()) {
    if (al.size() == 50)
        outputReverseLines();
    al.add(line);
}
outputReverseLines();

Where outputReverseLines is a method that reverses, prints and clears the list. 其中outputReverseLines是一种用于反转,打印和清除列表的方法。

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

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