Possible Duplicate:
In Java how do a read/convert an InputStream in to a string?
Hi, I want to convert this BufferedInputStream into my string. How can I do this?
BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream() ); String a= in.read();
To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.
With a BufferedInputStream , the method delegates to an overloaded read() method that reads 8192 amount of bytes and buffers them until they are needed. It still returns only the single byte (but keeps the others in reserve). This way the BufferedInputStream makes less native calls to the OS to read from the file.
Key differences: BufferedInputStream is buffered, but FileInputStream is not. A BufferedInputStream reads from another InputStream , but a FileInputStream reads from a file1.
The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes. The Java BufferedReader class is a subclass of the Java Reader class, so you can use a BufferedReader anywhere a Reader is required.
BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream()); byte[] contents = new byte[1024]; int bytesRead = 0; String strFileContents; while((bytesRead = in.read(contents)) != -1) { strFileContents += new String(contents, 0, bytesRead); } System.out.print(strFileContents);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With