简体   繁体   English

如何使用Java从最后一行读取文件到第一行

[英]how to read file from last line to first using java

    FileInputStream fstream = new FileInputStream("\\file path");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
     while (br.ready()) {

                                line = br.readLine();
    }

Please let me know how to read a file from the last line to first provided the row number is not fixed and is varying with time? 如果行号不固定并且随时间变化,请让我知道如何从最后一行读取文件。 I know the above is useful for reading it from first row... 我知道以上内容对于从第一行读取内容很有用...

read the file into a list, and process that list backwards... files and streams are usually designed to work forward; 将文件读入列表,然后处理向后列出...文件和流通常旨在向前工作; so doing this directly with streams might turn out a lite awkward. 因此直接使用流执行此操作可能会导致轻度尴尬。 Only advised when the files are really huge... 仅在文件非常大时建议使用...

You cannot read a Buffer backwards, you can however count the lines of your buffer as explained in the link below 您不能向后读取缓冲区,但是可以按照以下链接中的说明计算缓冲区的行数

http://www.java2s.com/Code/Java/File-Input-Output/Countthenumberoflinesinthebuffer.htm http://www.java2s.com/Code/Java/File-Input-Output/Countbuffer.htm中的行数

And afterwards select your line using this code: 然后使用以下代码选择您的行:

FileInputStream fs= new FileInputStream("someFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 30; ++i)
  br.readLine();
String lineIWant = br.readLine();

As you can see, you iterate, reading each line(and doing nothing) before you get to the one you want (here we got 31 lines passed and #32 is the one read). 如您所见,您进行迭代,读取每一行(不执行任何操作),然后转到所需的行(此处我们传递了31行,而#32是被读取的行)。 If your file is huge this will take a lot of time. 如果文件很大,这将花费很多时间。

Other way to to this is to input everything in a List and then with a sizeof() and a for() you can select everything you want. 实现此目的的另一种方法是在列表中输入所有内容,然后使用sizeof()和for()选择所需的所有内容。

If you know the length of each line then you can work out how many lines there are by looking at the size of the file and dividing by the length of each line. 如果知道每一行的长度,则可以通过查看文件的大小并除以每一行的长度来算出多少行。 (this of course ignores any possible metadata in the file) (这当然会忽略文件中任何可能的元数据)

You can then use some maths to get the start byte of the last line. 然后,您可以使用一些数学运算来获取最后一行的起始字节。 Once you have then you can then open a RandomAccessFile on the file and then use seek to go to that point. 找到之后,您可以在文件上打开一个RandomAccessFile,然后使用seek转到该位置。 Then using readline you can then read the last line 然后使用readline,您可以阅读最后一行

This does assume though that the lines are all the same length. 确实假设这些线的长度都相同。

You can use FileUtils 您可以使用FileUtils

and use this method 并使用这种方法

static List<String> readLines(File file) 
          Reads the contents of a file line by line to a 
          List of Strings using the default encoding for the VM.

This will return a List then use Collections.reverse() 这将返回一个列表,然后使用Collections.reverse()

Then simply iterate it to get the file lines in reverse order 然后只需对其进行迭代即可以相反的顺序获取文件行

这可能对您有帮助[1]: http : //mattfleming.com/node/11

只需向后保存信息,这就是我所做的全部。只需阅读Pryor即可保存并使用\\ n

You can save the lines in a list (in my code a arraylist) and "read" the lines backwards from the arraylist: 您可以将行保存在列表中(在我的代码中为arraylist),然后从arraylist中“读取”行:

try
    {

        FileInputStream fstream = new FileInputStream("\\file path");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        //Read lines and save in ArrayList 
        while (br.ready())
        {
            lines.add(br.readLine());
        }

        //Go backwards through the ArrayList
        for (int i = lines.size(); i >= 0; i--)
        {
            line = lines.get(i);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

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

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