Java 读取文件,然后跳转到末尾

Java 读取文件,然后跳转到末尾,java,Java,我想读取文本文件,然后获取读取文件的偏移量。我尝试了下面的程序,但问题是我不想使用RandomAccessFile,我如何才能做到这一点 RandomAccessFile access = null; try { access = new RandomAccessFile(file, "r"); if (file.length() < addFileLen) {

我想读取文本文件,然后获取读取文件的偏移量。我尝试了下面的程序,但问题是我不想使用RandomAccessFile,我如何才能做到这一点

RandomAccessFile access = null;
                try {
                    access = new RandomAccessFile(file, "r");

                    if (file.length() < addFileLen) {
                        access.seek(file.length());
                    } else {
                        access.seek(addFileLen);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String line = null;
                try {

                    while ((line = access.readLine()) != null) {

                        System.out.println(line);
                        addFileLen = file.length();

                    }
RandomAccessFile access=null;
试一试{
access=新的随机访问文件(文件“r”);
if(file.length()
如果要连续读取文件,可以执行以下操作。这通过不实际读取文件结尾来实现。问题是,结尾可能没有完整的行,甚至没有完整的多字节字符

class FileUpdater {
    private static final long MAX_SIZE = 64 * 1024;
    private static final byte[] NO_BYTES = {};

    private final FileInputStream in;
    private long readSoFar = 0;

    public FileUpdater(File file) throws FileNotFoundException {
        this.in = new FileInputStream(file);
    }

    public byte[] read() throws IOException {
        long size = in.getChannel().size();
        long toRead = size - readSoFar;
        if (toRead > MAX_SIZE)
            toRead = MAX_SIZE;
        if (toRead == 0)
            return NO_BYTES;
        byte[] bytes = new byte[(int) toRead];
        in.read(bytes);
        readSoFar += toRead;
        return bytes;
    }    
}

如果您不想使用RandomAccessFile,您会选择什么?查找有关ByTechannels的信息,如果RandomAccessFile起作用,您不想使用它的原因是什么?问题是,我正在监视不断生成的日志文件,而该日志文件正在使用滚动文件追加器,它应该创建备份文件并随机访问文件阻碍了这一过程。我希望以行的形式获取数据,因为我希望解析整个日志行,并根据指定的严重性将其发送到邮件。您不认为这样做会有额外的开销。文件不能保证有整行,所以您不能假设有整行。相反,您需要自己扫描数据并保存字节,无需换行即可进行下一次读取。虽然开销很小,但与使用BufferedReader大致相同,与读取IO的成本相比非常小。