Java BufferedReader读取行返回null

Java BufferedReader读取行返回null,java,file,zip,bufferedreader,file-read,Java,File,Zip,Bufferedreader,File Read,我有一个zip文件,其中包含9个我想读取的文件。因此,我实现了以下功能: public static Map<String, BufferedReader> unzipFile(InputStream zippedFile) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(zippedFile); HashMap<String, BufferedReader> resu

我有一个zip文件,其中包含9个我想读取的文件。因此,我实现了以下功能:

public static Map<String, BufferedReader> unzipFile(InputStream zippedFile) throws IOException {
  ZipInputStream zipInputStream = new ZipInputStream(zippedFile);
  HashMap<String, BufferedReader> result = new HashMap<>(9);
  for (ZipEntry zipEntry = zipInputStream.getNextEntry(); zipEntry != null; zipEntry = zipInputStream.getNextEntry()) {
    if (zipEntry.isDirectory()) {
      continue;
    }
    result.put(zipEntry.getName(), new BufferedReader(new InputStreamReader(zipInputStream)));
  }
  return result;
}
我想可能与所创建变量的范围有关,因为在调试时,我注意到我可以在for循环中获取文件的内容。但是,我不想将这个zip提取方法与获取内容并对其进行解析的方法混合使用。此外,我不想将提取的文件保存到光盘中,然后再重新打开它们

那么,我怎样才能用BufferedReader来填充这个映射,而BufferedReader在readLine调用时不会返回null?

ZipInputStream 每次迭代条目指针时,都会丢失条目指针,因为每个ZipInputStream对象只允许一个流

try (ZipInputStream is = new ZipInputStream(Zippy.class.getResourceAsStream("file.zip"))) {
            ZipEntry entry;
            while ((entry = is.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    // do your logic here (get the entry name)
                }                
                is.closeEntry();
        }
    }
从javadocs: closeEntry-关闭当前ZIP条目并定位流以读取下一个条目

基本上,您在任何给定时间只能打开一个条目

因此,如果您想做您正在做的事情,最好保存zip条目的名称,并遍历ZipInputStream以将指针移动到正确的位置,然后可以使用ZipInputStream.read获取字节

压缩文件 如果您可以使用ZipFile对象而不是ZipInputStream,那么您就可以完成您想要完成的任务

 static void loadMap() throws IOException {
    ZipFile file = new ZipFile("testzip.zip");
    Enumeration<? extends ZipEntry> entries = file.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            streamMap.put(entry.getName(), new BufferedReader(new InputStreamReader(file.getInputStream(entry))));
        }

    }

}

public static void main(String[] args) throws IOException {
    loadMap();

    Iterator<BufferedReader> iter = streamMap.values().iterator();
    while (iter.hasNext()) {            
        BufferedReader reader = iter.next();
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}


OUTPUT:
file 2: First line!
file 2: Second Line!
file 1: First line!
file 1 : Second Line!
BUILD SUCCESSFUL (total time: 0 seconds)

只需记住按住zip文件引用并调用file.close;完成后。

测试中调用的方法会获取类似字符串的内容;这里显示的方法采用InputStream。请显示字符串重载,因为此处显示的方法无法返回null。添加了您要求的内容,@AndyTurnerI对我的示例进行了大量编辑,使其适用于您的BufferedReader要求。Cheese没有发现任何关于在任何给定时间只允许一次进入的内容。但是你的解释很有道理,而且帮了我很多忙!谢谢
try (ZipInputStream is = new ZipInputStream(Zippy.class.getResourceAsStream("file.zip"))) {
            ZipEntry entry;
            while ((entry = is.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    // do your logic here (get the entry name)
                }                
                is.closeEntry();
        }
    }
getNextEntry() - Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
 static void loadMap() throws IOException {
    ZipFile file = new ZipFile("testzip.zip");
    Enumeration<? extends ZipEntry> entries = file.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            streamMap.put(entry.getName(), new BufferedReader(new InputStreamReader(file.getInputStream(entry))));
        }

    }

}

public static void main(String[] args) throws IOException {
    loadMap();

    Iterator<BufferedReader> iter = streamMap.values().iterator();
    while (iter.hasNext()) {            
        BufferedReader reader = iter.next();
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}


OUTPUT:
file 2: First line!
file 2: Second Line!
file 1: First line!
file 1 : Second Line!
BUILD SUCCESSFUL (total time: 0 seconds)