Reading text files in a zip archive

后端 未结 1 1062
星月不相逢
星月不相逢 2020-12-28 17:18

I have zip archive that contains a bunch of plain text files in it. I want to parse each text files data. Here\'s what I\'ve written so far:

try {
    fina         


        
相关标签:
1条回答
  • 2020-12-28 17:25

    No, you don't need a RandomAccessFile. First get an InputStream with the data for this zip file entry:

    InputStream input = zipFile.getInputStream(entry);
    

    Then wrap it in an InputStreamReader (to decode from binary to text) and a BufferedReader (to read a line at a time):

    BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    

    Then read lines from it as normal. Wrap all the appropriate bits in try/finally blocks as usual, too, to close all resources.

    0 讨论(0)
提交回复
热议问题