我正在逐行读取文件。该文件由 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/