第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

BufferedReader readLine 返回 null

BufferedReader readLine 返回 null

慕碼人2483693 2022-10-07 19:35:33
我有一個(gè) zip 文件,其中包含我想要閱讀的 9 個(gè)文件。所以我實(shí)現(xiàn)了以下功能: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;}同一類中的其他簽名(調(diào)用相同的方法):  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從該方法中獲取返回的值,并能夠在我的代碼的其他地方讀取它。問題是每次我在此方法的循環(huán)result.get("filename").readLine()外調(diào)用時(shí),它都會(huì)返回.fornull這是此方法的簡(jiǎn)單單元測(cè)試 - 目前在最后一個(gè)失敗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());  }我想可能與創(chuàng)建的變量的范圍有關(guān),因?yàn)樵谡{(diào)試時(shí),我注意到我可以在for循環(huán)中獲取文件的內(nèi)容。但是,我不想將這種 zip 提取方法與獲取內(nèi)容并解析它的方法混合使用。此外,我不想將提取的文件保存到光盤并稍后重新打開它們。那么,我怎樣才能用不會(huì)在調(diào)用時(shí)返回 null 的 s 來填充Map它BufferedReader呢readLine?
查看完整描述

1 回答

?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

ZipInputStream

每次迭代入口指針時(shí),都會(huì)丟失入口指針,因?yàn)槊總€(gè) ZipInputStream 對(duì)象只允許一個(gè)流。

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.

基本上,您在任何給定時(shí)間只能打開一個(gè)條目。


因此,如果你想做你正在做的事情,你能做的最好的就是保存 zip 條目的名稱并遍歷 ZipInputStream 以將指針移動(dòng)到正確的位置,然后你可以使用 ZipInputStream.read() 來獲取字節(jié)。


壓縮文件

如果您可以使用 ZipFile 對(duì)象(而不是 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();在完成后調(diào)用。


查看完整回答
反對(duì) 回復(fù) 2022-10-07
  • 1 回答
  • 0 關(guān)注
  • 261 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)