亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

BufferedReader readLine 返回 null

BufferedReader readLine 返回 null

慕碼人2483693 2022-10-07 19:35:33
我有一個 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;}同一類中的其他簽名(調用相同的方法):  public static Map<String, BufferedReader> unzipFile(String zippedFile) throws IOException {    return unzipFile(new File(zippedFile));  }  public static Map<String, BufferedReader> unzipFile(File zippedFile) throws IOException {    return unzipFile(new FileInputStream(zippedFile));  }我的想法是Map從該方法中獲取返回的值,并能夠在我的代碼的其他地方讀取它。問題是每次我在此方法的循環result.get("filename").readLine()外調用時,它都會返回.fornull這是此方法的簡單單元測試 - 目前在最后一個失敗Assert.assertNotNull:  @Test  public void unzipFileTest() throws Exception  {    Map<String, BufferedReader> result = FileHandlerUtility.unzipFile(TestUtil.FILE_SAMPLE_NAME);    Assert.assertNotNull(result);    Assert.assertEquals(result.size(), 9);    Assert.assertNotNull(result.get("Car.txt"));    Assert.assertNotNull(result.get("Client.txt"));    Assert.assertNotNull(result.get("Client.txt").readLine());  }我想可能與創建的變量的范圍有關,因為在調試時,我注意到我可以在for循環中獲取文件的內容。但是,我不想將這種 zip 提取方法與獲取內容并解析它的方法混合使用。此外,我不想將提取的文件保存到光盤并稍后重新打開它們。那么,我怎樣才能用不會在調用時返回 null 的 s 來填充Map它BufferedReader呢readLine?
查看完整描述

1 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

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() -Closes the current ZIP entry and positions the stream for reading the next entry.


getNextEntry() - Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

基本上,您在任何給定時間只能打開一個條目。


因此,如果你想做你正在做的事情,你能做的最好的就是保存 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();在完成后調用。


查看完整回答
反對 回復 2022-10-07
  • 1 回答
  • 0 關注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號