java - 获取 BufferedReader 读取的文件中的偏移量?

标签 java io filesize bufferedreader

我正在逐行读取文件。该文件由 CipherOutputStream 编码,然后由 DeflaterOutputStream 压缩。该文件可以包含 UTF-8 字符,如俄语字母等。

我想获取实际读取文件中的偏移量,或者br.ReadLine()命令读取的字节数。问题是文件既加密又放气,因此读取字符串的长度大于文件中读取的字节数。

            InputStream fis=tempURL.openStream();                       //in tempURL I've got an URL to download
            CipherInputStream cis=new CipherInputStream(fis,pbeCipher); //CipherStream
            InflaterInputStream iis=new InflaterInputStream(cis);       //InflaterInputStream
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(iis, "UTF8"));                //BufferedReader
            br.readLine();

            int fSize=tempURL.openConnection().getContentLength();      //Catch FileSize

最佳答案

使用 CountingInputStream来自 Apache Commons IO项目:

InputStream fis=tempURL.openStream();
CountingInputStream countStream = new CountingInputStream(fis);
CipherInputStream cis=new CipherInputStream(countStream,pbeCipher);
...

稍后您可以使用 countStream.getByteCount() 获取文件位置。

关于java - 获取 BufferedReader 读取的文件中的偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189034/

相关文章:

java - 让线程读取共享变量直到被修改

java - 在 Java 中处理 IO 异常

python-2.7 - Python 结构错误 : unpack requires a string argument of length 2

c - 在 C 中获取文件大小的正确方法

c# - 不使用 System.IO.FileInfo 获取文件大小?

java - (Android) 如何获取智能手机的屏幕宽度和高度?

java - Java 中的谓词搜索

java - 如何使用 opencsv 将整数值写入 csv 文件

c - C如何区分一个字节长的字符和一个2字节长的字符?

C# 无法将 FileSize 从字节转换为 GB