|
| |||||||||
| Tags: class, date, java, object, program, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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. |
|
#2
| ||||
| ||||
| 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} |
|
#3
| |||
| |||
| 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();
} |
|
#4
| ||||
| ||||
| 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();
} |
|
#5
| ||||
| ||||
| 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();
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |