简体   繁体   English

阅读Java输入流的第一部分

[英]Read first part of inputstream in Java

I have a XML file that I read from a URLConnection. 我有一个从URLConnection读取的XML文件。 The file grows as time goes. 该文件随着时间的流逝而增长。 I want to read the first 100k of the file, today the file is 1.3MB. 我想读取文件的前100k,今天文件为1.3MB。 Then I want to parse the part I have read. 然后,我想解析我已阅读的部分。 How can I do that? 我怎样才能做到这一点?

(From scratch) (从头开始)

int length = 100*1024;
byte[] buf = new byte[length];
urlConnection.getInputStream().read(buf,0,length);
StringBufferInputStream in = new StringBufferInputStream(new String(buf));
new SAXParser().parse(in,myHandler);

As far as I understand you're interested not just in 100k of a stream but 100k of a stream from which you could extract data you need. 据我了解,您不仅对100k的流感兴趣,还对100k的流感兴趣,可以从中提取所需的数据。 This means taking 100k as proposed by Peter won't work as it might result in non-well-formed XML. 这意味着彼得提出的100k无效,因为这可能会导致XML格式不正确。

Instead I'd suggest to use StAX parser which will give you ability to read and parse XML directly from stream with ability to stop when you've reached 100k (or near) limit. 相反,我建议使用StAX解析器,它使您能够直接从流中读取和解析XML,并能够在达到100k(或接近)限制时停止。

For further information take a look at XMLStreamReader interface (and samples around its usage). 有关更多信息,请查看XMLStreamReader接口(及其用法示例)。 For example you could loop until you get to the START_ELEMENT with name "result" and then use method getTextCharacters(int sourceStart, char[] target, int targetStart, int length) specifying 100k as buffer size. 例如,您可以循环播放直到名称为“ result”的START_ELEMENT ,然后使用方法getTextCharacters(int sourceStart, char[] target, int targetStart, int length)指定100k作为缓冲区大小。

As you mentioned Android currently it doesn't have StAX parser available. 如您所提到的,Android当前没有 StAX解析器。 However it does have XmlPullParser with similar functionality. 但是,它确实具有具有类似功能的XmlPullParser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM