我正在尝试从ZIP存档中读取一个XML文件。有关守则如下:
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
if(entry.getName().equals("plugin.xml")) {
int size = (int)entry.getSize();
byte[] bytes = new byte[size];
int read = zis.read(bytes, 0, size);
System.out.println("File size: " + size);
System.out.println("Bytes read: " + read);
}
}这一点,当工作产生的输出如下:
File size: 5224
Bytes read: 5224正在读取的plugin.xml文件没有什么特别之处,并且通过了我能找到的对XML文件的任何验证(删除字符、添加字符等)。有时,从输入流中读取的“字节”小于文件大小的情况。在本例中,我更改了相同文件的XML属性的文本值,并得到了以下结果:
File size: 5218
Bytes read: 5205 // the reader stopped early!我看不出XML文件的工作模式和不工作模式,它看起来完全是随机的。
以前有没有人遇到过这样的事?
编辑:忘了提到,读取plugin.xml文件的plugin.xml代码嵌入到现成的应用程序中,我无法更改。我的问题是试图理解为什么在某些情况下它不接受我的XML文件。
发布于 2012-11-01 23:02:29
它说InputStream.read()或它的任何实现或重写在哪里填充缓冲区?检查一下Javadoc。实际上,read()要么返回指示EOS的-1,要么将至少一个字节读入缓冲区。你得绕圈。
发布于 2018-02-22 21:54:46
如前所述,您需要使用循环。我必须解决这个确切的问题,所以我想我会张贴一个例子。
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
if(entry.getName().equals("plugin.xml")) {
int size = (int)entry.getSize();
byte[] bytes = new byte[size];
int read = 0;
while (read < size) {
read += zis.read(bytes, read, (size - read));
}
System.out.println("File size: " + size);
System.out.println("Bytes read: " + read);
}
}https://stackoverflow.com/questions/13186807
复制相似问题