There is an archive in it a set of files, you need to read the files from the archive and selectively take one of them, you do not need to unzip the archive to disk, you want to load individual files into streams and then work with them.

What I tried:

ZipFile zf=new ZipFile("src/pack/text.zip") //загружал архив Enumeration entries = zf.entries()//собирал список файлов while(entries.hasMoreElements()) { //проходил по всем элементам архива ZipEntry entry=(ZipEntry)entries.nextElement();//брал отдельный файл InputStream stream=new InputStream()//создавал поток stream=zf.getInputStream(entry);//пихал туда отдельный файл 

I would like to know what is recorded in the streams, only the internal file?

How to continue to use the recorded data in the stream? How then to write down for example in a line the contents of the file.
I read about the conversion of bytes, it is not clear enough, it is necessary to conjure with encodings
The kettle will be happy for any help.

  • You have too many questions ... Flows are a huge topic, it is impossible to express it as part of the answer to a question. I would recommend you start with mastering a class scanner. It is easy to use. In addition, read about such a pattern as an adapter, because the use of streams shows its good qualities in the context of this pattern. Those. when you create a stream that accepts input, for example, a class file, then you buffer it, then you do something else, then this is the adapter. Such designs are similar to pipes and manifolds. You collect about what you need. - Dmitriy
  • Thanks for the answer, there are many questions because when it’s not clear, it’s hard to know where to start - justdoit
  • From the scanner. Start with it, you will not immediately master the streams. Write a program for console input from the keyboard. And then to read from a text file. - Dmitriy

1 answer 1

The main steps of working with threads in Java: open a stream for reading / writing, read its contents or write something to a stream, close the stream. For easier reading of text files, the InputStream with the contents of the file is usually wrapped in a BufferedReader or Scanner . See an example of reading a java text file .

And this is how you can use the standard Java tools to get the entire InputStream as a string:

 private static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } 

This trick is borrowed from the article "Stupid Scanner tricks" . His work is based on the fact that the Scanner passes through the entire flow dividing it into parts using the "beginning of the input boundary" , \A ), ie returns the entire contents of the stream in one piece.

Note that you can specify the encoding used for the input stream by the second argument in the Scanner constructor (for example, "UTF-8").

  • Thank you, everything works, one less question) - justdoit
  • @justdoit do not thank the author of the answer in the comments. To thank the author of the answer, vote for the answer or mark it as a decision. From help: What to do with the answers to my question? - naXa