It is necessary to read a text file from a stream, as it is done with a picture.
Important: reading in the OP and not in the file.

ZipInputStream zis; BufferedImage tmpImage; tmpImage = null; tmpImage = ImageIO.read(zis); 
  • 2
    Nichrome did not understand, and where are the pictures and what is OP. The task is to, having an arbitrary InputStream, read the text from it to get the output (String) on ​​the output? - Nikolay Artamonov
  • It seems that he showed a sample, and the same to him only with a text file. OP possible RAM =) - Farhod
  • You are right =) OP = RAM. Yes, count in String if there is no special class for text files. Preferably not manually cycle - ckesc
  • To @ckesc. Explain properly. You have an open stream associated with the file, but you do not know the name of this file. And you want to read the contents of the file in a String without cycles and a possible extension (of course creating a new object) String. So what? - avp
  • @avp No Loaded over the network zip archive. it is unpacked by the ZipInutStream stream, and from it you can already get the compressed data. I want to save it on the fly, for example, in String for the subsequent display to the user. It is important not to mess with line breaks and encoding. Saving to a temporary file is not desirable. This applet is ckesc

1 answer 1

Using only classes from the standard Java library, avoiding loops and other govnokoda does not work. Having an arbitrary InputStream, you can read the text as a string from it as follows:

 import java.nio.charset.Charset; import java.io.*; static public String readTextFromInputStream(InputStream in, Charset cs) { StringBuilder text = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, cs)); String line = null; String newline = System.getProperty("line.separator"); while ((line = reader.readLine()) != null) { text.append(line); text.append(newline); } return text.toString(); } 

Where the parameter cs means the intended encoding of the text. Use something like this:

 String text = readTextFromInputStream(in, Charset.forName("CP1251")); 

If you want to avoid all this govnokod, you have two options: either connect external libraries (for example, Apache Commons IO ), or use normal languages ​​for JVM, for example, Scala , in which the solution will take one line:

 def readFromInputStream(in: InputStream, cs: Codec) = Source.fromInputStream(in)(cs).mkString 

Addition 1 . If you want to keep the splitting into strings, then we modify the original version of the readFromInputStream function as follows (so that it returns a list of strings):

 import java.util.List; import java.util.ArrayList; static public List<String> readLinesFromInputStream(InputStream in, Charset cs) { List<String> lines = new ArrayList(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, cs)); String line = null; String newline = System.getProperty("line.separator"); while ((line = reader.readLine()) != null) { lines.add(line + newline); } return lines; } 
  • Great! Is automatic encoding detection possible? Is there a class to immediately store splitting? Or you need to do * or an array of strings * or place html tags (as far as I know jLabel supports them) - ckesc
  • This will have to use third-party libraries. For example, juniversalchardet ( code.google.com/p/juniversalchardet ) or jchardet ( jchardet.sourceforge.net ). On stackoverflow.com saw a review that the second one more accurately defines encodings, at least Latin ones. - Nikolay Artamonov
  • one
    Completed the answer on the question of preserving the line splitting. - Nikolay Artamonov