我正在使用 StAX API 处理大型 (1TB) XML 文件。假设我们有一个循环处理一些元素:
XMLInputFactory fac = XMLInputFactory.newInstance();
XMLStreamReader reader = fac.createXMLStreamReader(new FileReader(inputFile));
while (true) {
if (reader.nextTag() == XMLStreamConstants.START_ELEMENT){
// handle contents
}
}
如何跟踪大型 XML 文件中的总体进度?对于较小的文件,从读取器获取偏移量效果很好:
int offset = reader.getLocation().getCharacterOffset();
但作为整数偏移量,它可能只适用于最大 2GB 的文件...
最佳答案
一个简单的FilterReader
应该可以工作。
class ProgressCounter extends FilterReader {
long progress = 0;
@Override
public long skip(long n) throws IOException {
progress += n;
return super.skip(n);
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int red = super.read(cbuf, off, len);
progress += red;
return red;
}
@Override
public int read() throws IOException {
int red = super.read();
progress += red;
return red;
}
public ProgressCounter(Reader in) {
super(in);
}
public long getProgress () {
return progress;
}
}
关于java - 如何在 StAX 中跟踪大文件的解析进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34724494/