Read string from Stream in Java
Description
The following code shows how to read string from Stream.
Example
/*w w w . ja va2 s . c om*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
public static String readString(InputStream stream) throws IOException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
int readedBytes;
byte[] buf = new byte[1024];
while ((readedBytes = stream.read(buf)) > 0) {
b.write(buf, 0, readedBytes);
}
b.close();
return b.toString();
}
}
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »