gpt4 book ai didi

java - 为什么在 ObservableMap 中引用 InputStream 实例时会关闭?

转载 作者:行者123 更新时间:2023-11-30 08:02:59 24 4
gpt4 key购买 nike

我有 ObservableMap,其中添加了资源文件。

private ObservableMap<String, InputStream> resourceFilesData;
resourceFilesData = new ObservableMapWrapper<String, InputStream>(
new HashMap<String, InputStream>()
);

并以这种方式添加InputStreams:

resourceFilesData.put(f.getName(), new FileInputStream(f));

最后,当我想使用流时,它们显示为关闭!

为什么?我找不到理由。也许,当流关闭时,有一些乳清来处理时刻? (用于调试)

如何使用流:

private void pack() throws JAXBException, IOException {
HashMap<String, InputStream> resources = new HashMap<>();
byte[] buf = new byte[1024];
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("../" + fwData.getFileName() + ".iolfw"));
File xml = fwData.marshal();
InputStream xmlStream = new FileInputStream(xml);

resources.put(xml.getName(), xmlStream);
resources.putAll(resourceFilesData);
for (Map.Entry<String, InputStream> data: resources.entrySet()) {
InputStream input = data.getValue();
zos.putNextEntry(new ZipEntry(data.getKey()));

for (int readNum = 0; (readNum = input.read(buf)) != -1; ) {
zos.write(buf, 0, readNum);
}
zos.closeEntry();
input.close();
}
zos.close();
resources.remove(xmlStream);
xml.delete();
}

跟踪: http://pastebin.com/hE21ECL9

最佳答案

我不知道这种行为的原因。但您可以尝试使用继承类来调试问题:

class FileInputStreamInh extends FileInputStream {

public FileInputStreamInh(File file) throws FileNotFoundException {
super(file);
}

@Override
public void close() throws IOException {
super.close();
^^^breakpoint here
}
}

因此,您应该创建 FileInputStreamInh,而不是创建 FileInputStream

关于java - 为什么在 ObservableMap 中引用 InputStream 实例时会关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com