如何在Java中从末尾到开头(反向顺序)读取文件?

28

我想从文件的末尾开始向前读取文件内容。

[1322110800] LOG ROTATION: DAILY
[1322110800] LOG VERSION: 2.0
[1322110800] CURRENT HOST STATE:arsalan.hussain;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.107)
[1322110800] CURRENT HOST STATE: localhost;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.06 ms
[1322110800] CURRENT HOST STATE: musewerx-72c7b0;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.27 ms

我用代码以这种方式读取它:

String strpath="/var/nagios.log";
FileReader fr = new FileReader(strpath);
BufferedReader br = new BufferedReader(fr);
String ch;
int time=0;
String Conversion="";
do {
    ch = br.readLine();
    out.print(ch+"<br/>"); 
} while (ch != null);
fr.close();

我更喜欢使用缓冲读取器以相反的顺序阅读


1
请查看以下链接:https://dev59.com/tm025IYBdhLWcg3wbVan - Zlatan
我们如何使用反向排序方法? - Salman Raza
我看过那篇帖子,但解决方案太大了,我需要一个紧急且小巧的解决方案来实现。 - Salman Raza
@SalmanRaza - 为什么你需要一个简单的解决方案?为什么不能根据下面和链接问题中的答案自己实现一个解决方案?请记住,SO不是一个“为我编写代码”的服务。 - Stephen C
显示剩余2条评论
7个回答

64

我遇到了与这里描述的问题相同的情况。我想要从文件末尾开始逆序查看行(unix tac 命令可以实现此功能)。但是我的输入文件相当大,因此像其他示例中那样将整个文件读入内存并不可行。

下面是我想出的类,它确实使用了RandomAccessFile,但不需要任何缓冲区,因为它只保留对文件本身的指针,并使用标准的InputStream方法进行操作。

它适用于我的案例,以及我尝试过的空文件和其他一些东西。现在我没有Unicode字符或任何花哨的东西,但只要行由LF分隔,即使它们有LF + CR,它也应该可以工作。

基本用法如下:

in = new BufferedReader (new InputStreamReader (new ReverseLineInputStream(file)));

while(true) {
    String line = in.readLine();
    if (line == null) {
        break;
    }
    System.out.println("X:" + line);
}

这是主要来源:

package www.kosoft.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class ReverseLineInputStream extends InputStream {

    RandomAccessFile in;

    long currentLineStart = -1;
    long currentLineEnd = -1;
    long currentPos = -1;
    long lastPosInFile = -1;

    public ReverseLineInputStream(File file) throws FileNotFoundException {
        in = new RandomAccessFile(file, "r");
        currentLineStart = file.length();
        currentLineEnd = file.length();
        lastPosInFile = file.length() -1;
        currentPos = currentLineEnd; 
    }

    public void findPrevLine() throws IOException {

        currentLineEnd = currentLineStart; 

        // There are no more lines, since we are at the beginning of the file and no lines.
        if (currentLineEnd == 0) {
            currentLineEnd = -1;
            currentLineStart = -1;
            currentPos = -1;
            return; 
        }

        long filePointer = currentLineStart -1;

         while ( true) {
             filePointer--;

            // we are at start of file so this is the first line in the file.
            if (filePointer < 0) {  
                break; 
            }

            in.seek(filePointer);
            int readByte = in.readByte();

            // We ignore last LF in file. search back to find the previous LF.
            if (readByte == 0xA && filePointer != lastPosInFile ) {   
                break;
            }
         }
         // we want to start at pointer +1 so we are after the LF we found or at 0 the start of the file.   
         currentLineStart = filePointer + 1;
         currentPos = currentLineStart;
    }

    public int read() throws IOException {

        if (currentPos < currentLineEnd ) {
            in.seek(currentPos++);
            int readByte = in.readByte();
            return readByte;

        }
        else if (currentPos < 0) {
            return -1;
        }
        else {
            findPrevLine();
            return read();
        }
    }
}

3
虽然你自己编写代码很值得赞扬,但你需要某种形式的缓冲区来提高性能,否则逐个字节读取的性能会很差。https://dev59.com/_moy5IYBdhLWcg3wQ74F#31961274 可能是更好的选择。我也手写了一个解决方案,但现在不得不放弃它 :) - Gili
@Gili 那个解决方案在哪里? - steveen zoleko
@steveenzoleko 已删除代码,因为其性能太差。如果性能不是问题,您可以使用上面的解决方案作为替代。 - Gili

30

现在Apache Commons IO已经有了ReversedLinesFileReader类,用于这种情况(从版本2.2开始)。

所以你的代码可以是:

String strpath="/var/nagios.log";
ReversedLinesFileReader fr = new ReversedLinesFileReader(new File(strpath));
String ch;
int time=0;
String Conversion="";
do {
    ch = fr.readLine();
    out.print(ch+"<br/>"); 
} while (ch != null);
fr.close();

12

当你尝试读取数千行时,所提出的ReverseLineInputStream效率非常。在我的PC上,即Intel Core i7固态硬盘上,大约需要80秒才能读取60k行。这里是一个优化版本,使用了缓冲读取(与ReverseLineInputStream的一次读取一个字节相反)。60000行日志文件在400毫秒内读取:

public class FastReverseLineInputStream extends InputStream {

private static final int MAX_LINE_BYTES = 1024 * 1024;

private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024;

private RandomAccessFile in;

private long currentFilePos;

private int bufferSize;
private byte[] buffer;
private int currentBufferPos;

private int maxLineBytes;
private byte[] currentLine;
private int currentLineWritePos = 0;
private int currentLineReadPos = 0;
private boolean lineBuffered = false;

public ReverseLineInputStream(File file) throws IOException {
    this(file, DEFAULT_BUFFER_SIZE, MAX_LINE_BYTES);
}

public ReverseLineInputStream(File file, int bufferSize, int maxLineBytes) throws IOException {
    this.maxLineBytes = maxLineBytes;
    in = new RandomAccessFile(file, "r");
    currentFilePos = file.length() - 1;
    in.seek(currentFilePos);
    if (in.readByte() == 0xA) {
        currentFilePos--;
    }
    currentLine = new byte[maxLineBytes];
    currentLine[0] = 0xA;

    this.bufferSize = bufferSize;
    buffer = new byte[bufferSize];
    fillBuffer();
    fillLineBuffer();
}

@Override
public int read() throws IOException {
    if (currentFilePos <= 0 && currentBufferPos < 0 && currentLineReadPos < 0) {
        return -1;
    }

    if (!lineBuffered) {
        fillLineBuffer();
    }


    if (lineBuffered) {
        if (currentLineReadPos == 0) {
            lineBuffered = false;
        }
        return currentLine[currentLineReadPos--];
    }
    return 0;
}

private void fillBuffer() throws IOException {
    if (currentFilePos < 0) {
        return;
    }

    if (currentFilePos < bufferSize) {
        in.seek(0);
        in.read(buffer);
        currentBufferPos = (int) currentFilePos;
        currentFilePos = -1;
    } else {
        in.seek(currentFilePos);
        in.read(buffer);
        currentBufferPos = bufferSize - 1;
        currentFilePos = currentFilePos - bufferSize;
    }
}

private void fillLineBuffer() throws IOException {
    currentLineWritePos = 1;
    while (true) {

        // we've read all the buffer - need to fill it again
        if (currentBufferPos < 0) {
            fillBuffer();

            // nothing was buffered - we reached the beginning of a file
            if (currentBufferPos < 0) {
                currentLineReadPos = currentLineWritePos - 1;
                lineBuffered = true;
                return;
            }
        }

        byte b = buffer[currentBufferPos--];

        // \n is found - line fully buffered
        if (b == 0xA) {
            currentLineReadPos = currentLineWritePos - 1;
            lineBuffered = true;
            break;

            // just ignore \r for now
        } else if (b == 0xD) {
            continue;
        } else {
            if (currentLineWritePos == maxLineBytes) {
                throw new IOException("file has a line exceeding " + maxLineBytes
                        + " bytes; use constructor to pickup bigger line buffer");
            }

            // write the current line bytes in reverse order - reading from
            // the end will produce the correct line
            currentLine[currentLineWritePos++] = b;
        }
    }
}}

12

上面发布的ReverseLineInputStream恰好符合我的要求。我正在读取的文件很大,不能被缓存。

有一些错误:

  • 文件没有关闭
  • 如果最后一行没有终止符,则在第一次读取时会返回最后2行。

这是已更正的代码:

package www.kosoft.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class ReverseLineInputStream extends InputStream {

    RandomAccessFile in;

    long currentLineStart = -1;
    long currentLineEnd = -1;
    long currentPos = -1;
    long lastPosInFile = -1;
    int lastChar = -1;


    public ReverseLineInputStream(File file) throws FileNotFoundException {
        in = new RandomAccessFile(file, "r");
        currentLineStart = file.length();
        currentLineEnd = file.length();
        lastPosInFile = file.length() -1;
        currentPos = currentLineEnd; 

    }

    private void findPrevLine() throws IOException {
        if (lastChar == -1) {
            in.seek(lastPosInFile);
            lastChar = in.readByte();
        }

        currentLineEnd = currentLineStart; 

        // There are no more lines, since we are at the beginning of the file and no lines.
        if (currentLineEnd == 0) {
            currentLineEnd = -1;
            currentLineStart = -1;
            currentPos = -1;
            return; 
        }

        long filePointer = currentLineStart -1;

        while ( true) {
            filePointer--;

            // we are at start of file so this is the first line in the file.
            if (filePointer < 0) {  
                break; 
            }

            in.seek(filePointer);
            int readByte = in.readByte();

            // We ignore last LF in file. search back to find the previous LF.
            if (readByte == 0xA && filePointer != lastPosInFile ) {   
                break;
            }
        }
        // we want to start at pointer +1 so we are after the LF we found or at 0 the start of the file.   
        currentLineStart = filePointer + 1;
        currentPos = currentLineStart;
    }

    public int read() throws IOException {

        if (currentPos < currentLineEnd ) {
            in.seek(currentPos++);
            int readByte = in.readByte();            
            return readByte;
        } else if (currentPos > lastPosInFile && currentLineStart < currentLineEnd) {
            // last line in file (first returned)
            findPrevLine();
            if (lastChar != '\n' && lastChar != '\r') {
                // last line is not terminated
                return '\n';
            } else {
                return read();
            }
        } else if (currentPos < 0) {
            return -1;
        } else {
            findPrevLine();
            return read();
        }
    }

    @Override
    public void close() throws IOException {
        if (in != null) {
            in.close();
            in = null;
        }
    }
}

3
@Test
public void readAndPrintInReverseOrder() throws IOException {

    String path = "src/misctests/test.txt";

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(path));
        Stack<String> lines = new Stack<String>();
        String line = br.readLine();
        while(line != null) {
            lines.push(line);
            line = br.readLine();
        }

        while(! lines.empty()) {
            System.out.println(lines.pop());
        }

    } finally {
        if(br != null) {
            try {
                br.close();   
            } catch(IOException e) {
                // can't help it
            }
        }
    }
}

请注意,此代码将整个文件读入内存,然后开始打印。这是使用缓冲读取器或任何不支持寻址的其他读取器所能做的唯一方法。在你的情况下,你想要读取一个日志文件,日志文件可能非常大!如果您想逐行读取并实时打印,则没有其他选择,只能使用支持寻址的读取器,例如java.io.RandomAccessFile,但这绝非易事。

2

据我所知,您尝试逐行倒序读取。

假设这是您要读取的文件:

line1
line2
line3

您想将其按如下方式写入servlet的输出流:

line3
line2
line1

以下代码可能对此有所帮助:

    List<String> tmp = new ArrayList<String>();

    do {
        ch = br.readLine();
        tmp.add(ch);
        out.print(ch+"<br/>"); 
    } while (ch != null);

    for(int i=tmp.size()-1;i>=0;i--) {
        out.print(tmp.get(i)+"<br/>");
    }

7
我建议使用堆栈(Stack)集合,因为它是后进先出(LIFO)导向的。 - Mateusz Chromiński
5
下面的解决方案是正确的。但是,该解决方案需要将整个文件加载到内存中,这会浪费时间和空间。 - Igor Urisman

1

我对你的解决方案@dpetruha有问题,原因如下:

RandomAccessFile.read()从本地文件读取时是否保证确切的字节数被读取?

这是我的解决方案:(仅更改了fillBuffer)

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class ReverseLineInputStream extends InputStream {

    private static final int MAX_LINE_BYTES = 1024 * 1024;
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024;

    private RandomAccessFile in;
    private long currentFilePos;
    private int bufferSize;
    private byte[] buffer;
    private int currentBufferPos;
    private int maxLineBytes;
    private byte[] currentLine;
    private int currentLineWritePos = 0;
    private int currentLineReadPos = 0;
    private boolean lineBuffered = false;

    public ReverseLineInputStream(File file) throws IOException {
        this(file, DEFAULT_BUFFER_SIZE, MAX_LINE_BYTES);
    }

    public ReverseLineInputStream(File file, int bufferSize, int maxLineBytes) throws IOException {
        this.maxLineBytes = maxLineBytes;
        in = new RandomAccessFile(file, "r");
        currentFilePos = file.length() - 1;
        in.seek(currentFilePos);
        if (in.readByte() == 0xA) {
            currentFilePos--;
        }
        currentLine = new byte[maxLineBytes];
        currentLine[0] = 0xA;

        this.bufferSize = bufferSize;
        buffer = new byte[bufferSize];
        fillBuffer();
        fillLineBuffer();
    }

    @Override
    public int read() throws IOException {
        if (currentFilePos <= 0 && currentBufferPos < 0 && currentLineReadPos < 0) {
            return -1;
        }

        if (!lineBuffered) {
            fillLineBuffer();
        }

        if (lineBuffered) {
            if (currentLineReadPos == 0) {
                lineBuffered = false;
            }
            return currentLine[currentLineReadPos--];
        }
        return 0;
    }

    private void fillBuffer() throws IOException {
        if (currentFilePos < 0) {
            return;
        }

        if (currentFilePos < bufferSize) {
            in.seek(0);
            buffer = new byte[(int) currentFilePos + 1];
            in.readFully(buffer);
            currentBufferPos = (int) currentFilePos;
            currentFilePos = -1;
        } else {
            in.seek(currentFilePos - buffer.length);
            in.readFully(buffer);
            currentBufferPos = bufferSize - 1;
            currentFilePos = currentFilePos - bufferSize;
        }
    }

    private void fillLineBuffer() throws IOException {
        currentLineWritePos = 1;
        while (true) {

            // we've read all the buffer - need to fill it again
            if (currentBufferPos < 0) {
                fillBuffer();

                // nothing was buffered - we reached the beginning of a file
                if (currentBufferPos < 0) {
                    currentLineReadPos = currentLineWritePos - 1;
                    lineBuffered = true;
                    return;
                }
            }

            byte b = buffer[currentBufferPos--];

            // \n is found - line fully buffered
            if (b == 0xA) {
                currentLineReadPos = currentLineWritePos - 1;
                lineBuffered = true;
                break;

                // just ignore \r for now
            } else if (b == 0xD) {
                continue;
            } else {
                if (currentLineWritePos == maxLineBytes) {
                    throw new IOException("file has a line exceeding " + maxLineBytes
                            + " bytes; use constructor to pickup bigger line buffer");
                }

                // write the current line bytes in reverse order - reading from
                // the end will produce the correct line
                currentLine[currentLineWritePos++] = b;
            }
        }
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接