Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to convert InputStream to String in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-01-2010
Member
 
Join Date: Aug 2009
Posts: 51
How to convert InputStream to String in java?

Hello friends,
I am last year B.Sc.I.T.student. In our last exam there was one question like How to convert InputStream to String in java? I unable to write answer of this question. Please can anyone tell me any example of it to clear concept. Please help me.
Thanks in advanced.
Reply With Quote
  #2  
Old 20-01-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
Re: How to convert InputStream to String in java?

The following example will show you how to convert an InputStream to String. Hey ti is very easy process to do this. Just go through this code line by line and try to understand each steps.


Code:
package org.kodejava.example.io;
 
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class inputStreamToString {
 
    public static void main(String[] args) throws Exception {
        inputStreamToString ists = new inputStreamToString();
 
        
        InputStream ists = iists.getClass().getResourceAsStream("/data.txt");
 
        System.out.println(sts.convertinputStreamToString(ists));
    }
 
    public String convertinputStreamToString(InputStream ists) throws IOException {
       
        if (ists != null) {
            StringBuilder sb = new StringBuilder();
            String line;
 
            try {
                BufferedReader r1 = new BufferedReader(new InputStreamReader(ists, "UTF-8"));
                while ((line = r1.readLine()) != null) {
                    sb1.append(line).append("\n");
                }
            } finally {
                ists.close();
            }
            return sb1.toString();
        } else {       
            return "";
        }
    }
5}
Reply With Quote
  #3  
Old 20-01-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: How to convert InputStream to String in java?

Hey it is very easy process to convert InputStream to String in java. I have just written one example to convert InputStream to String in java. Just try to under stand each steps.

Code:
public static String inputStreamAsString(InputStream stream)
throws IOException {
BufferedReader BR = new BufferedReader(new InputStreamReader(stream1));
StringBuilder SB = new StringBuilder();
String line1 = null;

while ((line1 = BR.readLine()) != null) {
SB.append(line1 + "\n");
}

BR.close();
return SB.toString();
}
Reply With Quote
  #4  
Old 20-01-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: How to convert InputStream to String in java?

You can easily convert InputStream to String in java. You have to just take input from user and using following program you can convert InputStream to String. I think following example will clear you concept about converting InputStream to String.


Code:
      public static String pop (InputStream in) 
     throws IOException {
    StringBuffer put = new StringBuffer();
    byte[] B = new byte[5069];
    for (int N; (N = in.read(B)) != -1;) {
        put.append(new String(B, 0, N));
    }
    return put.toString();
}
Reply With Quote
  #5  
Old 20-01-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: How to convert InputStream to String in java?

I had written following code for converting InputStream to String in java and it is working properly in my pc. Just try to understand this code and try to compile in your computer.

Code:
public String parseISToString(java.io.InputStream ips)
{
java.io.DataInputStream di1 = new java.io.DataInputStream(ips);
StringBuffer sb = new StringBuffer();
try{
String line = null;
while((line=di1.readLine()) != null){
sbw.append(line+"\n");
}
}catch(Exception exw){
ex.getMessage();
}finally{
try{
ips.close();
}catch(Exception exw){}
}
return sbw.toString();
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert InputStream to String in java?"
Thread Thread Starter Forum Replies Last Post
How to convert string to char array in java? Baazigar Software Development 6 10-01-2011 06:36 PM
Convert string to binary in java TechGate Software Development 5 28-01-2010 01:36 PM
How to convert String to Date object in java? KALANI84 Software Development 4 20-01-2010 05:07 PM
How to convert string into int in Java Zool Software Development 3 09-11-2009 12:41 PM
how to convert a boolean value to a string in java? Janus Software Development 3 29-07-2009 09:07 PM


All times are GMT +5.5. The time now is 11:54 AM.