如果您有java.io.InputStream对象,应该如何处理该对象并生成String?
假设我有一个包含文本数据的InputStream,我想将其转换为String,例如,我可以将其写入日志文件。
获取InputStream并将其转换为字符串的最简单方法是什么?
public String convertStreamToString(InputStream is) {
// ???
}
如果您有java.io.InputStream对象,应该如何处理该对象并生成String?
假设我有一个包含文本数据的InputStream,我想将其转换为String,例如,我可以将其写入日志文件。
获取InputStream并将其转换为字符串的最简单方法是什么?
public String convertStreamToString(InputStream is) {
// ???
}
当前回答
这里或多或少有sampath的答案,清理了一点并表示为函数:
String streamToString(InputStream in) throws IOException {
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for(String line = br.readLine(); line != null; line = br.readLine())
out.append(line);
br.close();
return out.toString();
}
其他回答
我已经创建了这段代码,并且它有效。没有所需的外部插件。
有一个字符串到流和流到字符串的转换器:
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class STRINGTOSTREAM {
public static void main(String[] args)
{
String text = "Hello Bhola..!\nMy Name Is Kishan ";
InputStream strm = new ByteArrayInputStream(text.getBytes()); // Convert String to Stream
String data = streamTostring(strm);
System.out.println(data);
}
static String streamTostring(InputStream stream)
{
String data = "";
try
{
StringBuilder stringbuld = new StringBuilder();
int i;
while ((i=stream.read())!=-1)
{
stringbuld.append((char)i);
}
data = stringbuld.toString();
}
catch(Exception e)
{
data = "No data Streamed.";
}
return data;
}
尝试以下4种说法。。
根据Fred回忆的观点,不建议使用+=运算符附加String,因为每次将新字符附加到现有String时,都会再次创建一个新的String对象,并在旧的st对象变为垃圾时将其地址分配给st。
public String convertStreamToString(InputStream is)
{
int k;
StringBuffer sb=new StringBuffer();
while((k=fin.read()) != -1)
{
sb.append((char)k);
}
return sb.toString();
}
不建议,但这也是一种方式
public String convertStreamToString(InputStream is) {
int k;
String st="";
while((k=is.read()) != -1)
{
st+=(char)k;
}
return st;
}
如果使用流读取器,请确保在结束时关闭流
private String readStream(InputStream iStream) throws IOException {
//build a Stream Reader, it can read char by char
InputStreamReader iStreamReader = new InputStreamReader(iStream);
//build a buffered Reader, so that i can read whole line at once
BufferedReader bReader = new BufferedReader(iStreamReader);
String line = null;
StringBuilder builder = new StringBuilder();
while((line = bReader.readLine()) != null) { //Read till end
builder.append(line);
builder.append("\n"); // append new line to preserve lines
}
bReader.close(); //close all opened stuff
iStreamReader.close();
//iStream.close(); //EDIT: Let the creator of the stream close it!
// some readers may auto close the inner stream
return builder.toString();
}
编辑:在JDK7+上,可以使用trywithresources构造。
/**
* Reads the stream into a string
* @param iStream the input stream
* @return the string read from the stream
* @throws IOException when an IO error occurs
*/
private String readStream(InputStream iStream) throws IOException {
//Buffered reader allows us to read line by line
try (BufferedReader bReader =
new BufferedReader(new InputStreamReader(iStream))){
StringBuilder builder = new StringBuilder();
String line;
while((line = bReader.readLine()) != null) { //Read till end
builder.append(line);
builder.append("\n"); // append new line to preserve lines
}
return builder.toString();
}
}
以下内容并没有回答最初的问题,而是回答了一些问题。
几个回答暗示了这种形式的循环
String line = null;
while((line = reader.readLine()) != null) {
// ...
}
or
for(String line = reader.readLine(); line != null; line = reader.readLine()) {
// ...
}
第一种形式通过在封闭作用域中声明一个变量“read”来污染封闭作用域的命名空间,该变量不会用于for循环之外的任何内容。第二个表单重复readline()调用。
这里有一种用Java编写这种循环的更简洁的方法。事实证明,for循环中的第一个子句不需要实际的初始值设定项值。这将变量“line”的范围保持在for循环的主体内。更优雅!我在任何地方都没有看到有人使用这个表单(几年前的一天我随机发现了它),但我一直在使用它。
for (String line; (line = reader.readLine()) != null; ) {
//...
}
JDK 7/8应答,关闭流并仍然抛出IOException:
StringBuilder build = new StringBuilder();
byte[] buf = new byte[1024];
int length;
try (InputStream is = getInputStream()) {
while ((length = is.read(buf)) != -1) {
build.append(new String(buf, 0, length));
}
}