Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedInputStream To String Conversion? [duplicate]

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(); 
like image 813
Harinder Avatar asked Apr 19 '11 08:04

Harinder


People also ask

How do I change InputStream to string?

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.

Why is BufferedInputStream faster?

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.

What is the difference between BufferedInputStream and FileInputStream?

Key differences: BufferedInputStream is buffered, but FileInputStream is not. A BufferedInputStream reads from another InputStream , but a FileInputStream reads from a file1.

What is the difference between BufferedReader and BufferedInputStream?

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.


1 Answers

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); 
like image 73
Nirmal- thInk beYond Avatar answered Sep 21 '22 09:09

Nirmal- thInk beYond