【问题标题】:How to read file from end to start (in reverse order) in Java?如何在Java中从头到尾(以相反的顺序)读取文件?
【发布时间】:2011-12-29 06:34:14
【问题描述】:

我想从头到尾以相反的方向读取文件,

[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();

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

【问题讨论】:

标签: java file


【解决方案1】:

我遇到了与此处描述的相同的问题。我想以相反的顺序查看文件中的行,从结尾到开头(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();
        }
    }
}

【讨论】:

  • 虽然您自己编写代码令人钦佩,但您需要某种形式的缓冲;否则,一次读取一个字节的性能会很差。 stackoverflow.com/a/31961274/14731 可能是更好的选择。我还手动编写了一个解决方案,现在我不得不放弃它:)
  • @Gili 解决方案在哪里?
  • @steveenzoleko 走了。我删除了代码,因为它的性能太差了。如果性能不是问题,您可以使用上述解决方案作为替代方案。
【解决方案2】:

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();

【讨论】:

    【解决方案3】:

    上面发布的 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;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      当您尝试读取数千行时,建议的 ReverseLineInputStream 确实。在我的 PC SSD 驱动器上的 Intel Core i7 上,80 秒内大约有 60k 行。这是带有缓冲读取的启发式优化版本(与 ReverseLineInputStream 中的一次一个字节读取相反)。在 400 毫秒内读取 60k 行日志文件:

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

      【讨论】:

        【解决方案5】:

        据我了解,您尝试逐行向后阅读。 假设这是您尝试读取的文件:

        第一行
        第2行
        第3行

        而你想把它写到servlet的输出流中,如下:

        第3行
        第2行
        第1行

        以下代码在这种情况下可能会有所帮助:

            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/>");
            }
        

        【讨论】:

        • 我建议使用 Stack 集合,因为它是面向 LIFO 的
        • 这种方案需要把整个文件吸到内存中,既浪费时间又浪费空间。下面的解决方案是正确的。
        【解决方案6】:
        @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,这绝不是微不足道的。

        【讨论】:

          【解决方案7】:

          我对您的解决方案 @dpetruha 有疑问,因为:

          Does RandomAccessFile.read() from local file guarantee that exact number of bytes will be 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;
                      }
                  }
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-19
            • 1970-01-01
            • 2012-06-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多