GZIP输入逐行读取流
2022-08-31 11:20:19
我有一个.gz格式的文件。用于读取此文件的 java 类是 GZIPInputStream。但是,此类不会扩展 Java 的 BufferedReader 类。因此,我无法逐行读取文件。我需要这样的东西
reader = new MyGZInputStream( some constructor of GZInputStream)
reader.readLine()...
我创建了我的类,它扩展了Java的Reader或BufferedReader类,并使用GZIPInputStream作为其变量之一。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.zip.GZIPInputStream;
public class MyGZFilReader extends Reader {
private GZIPInputStream gzipInputStream = null;
char[] buf = new char[1024];
@Override
public void close() throws IOException {
gzipInputStream.close();
}
public MyGZFilReader(String filename)
throws FileNotFoundException, IOException {
gzipInputStream = new GZIPInputStream(new FileInputStream(filename));
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
// TODO Auto-generated method stub
return gzipInputStream.read((byte[])buf, off, len);
}
}
但是,当我使用时,这不起作用
BufferedReader in = new BufferedReader(
new MyGZFilReader("F:/gawiki-20090614-stub-meta-history.xml.gz"));
System.out.println(in.readLine());
有人可以建议如何继续..