Java中如何将DataInputStream转换为String?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3870847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert the DataInputStream to the String in Java?
提问by Questions
I want to ask a question about Java. I have use the URLConnection in Java to retrieve the DataInputStream. and I want to convert the DataInputStream into a String variable in Java. What should I do? Can anyone help me. thank you.
我想问一个关于Java的问题。我使用 Java 中的 URLConnection 来检索 DataInputStream。我想在 Java 中将 DataInputStream 转换为 String 变量。我该怎么办?谁能帮我。谢谢你。
The following is my code:
以下是我的代码:
URL data = new URL("http://google.com");
URLConnection dataConnection = data.openConnection();
DataInputStream dis = new DataInputStream(dataConnection.getInputStream());
String data_string;
// convent the DataInputStream to the String
采纳答案by Jigar Joshi
import java.net.*;
import java.io.*;
class ConnectionTest {
public static void main(String[] args) {
try {
URL google = new URL("http://www.google.com/");
URLConnection googleConnection = google.openConnection();
DataInputStream dis = new DataInputStream(googleConnection.getInputStream());
StringBuffer inputLine = new StringBuffer();
String tmp;
while ((tmp = dis.readLine()) != null) {
inputLine.append(tmp);
System.out.println(tmp);
}
//use inputLine.toString(); here it would have whole source
dis.close();
} catch (MalformedURLException me) {
System.out.println("MalformedURLException: " + me);
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
}
}
}
This is what you want.
这就是你想要的。
回答by Bozho
You can use commons-ioIOUtils.toString(dataConnection.getInputStream(), encoding)in order to achieve your goal.
您可以使用commons-ioIOUtils.toString(dataConnection.getInputStream(), encoding)来实现您的目标。
DataInputStreamis not used for what you want - i.e. you want to read the content of a website as String.
DataInputStream不用于您想要的内容 - 即您想将网站内容阅读为String.
回答by Grodriguez
If you want to read data from a generic URL (such as www.google.com), you probably don't want to use a DataInputStreamat all. Instead, create a BufferedReaderand read line by line with the readLine()method. Use the URLConnection.getContentType()field to find out the content's charset (you will need this in order to create your reader properly).
如果您想从通用 URL(例如www.google.com)读取数据,您可能根本不想使用 a DataInputStream。相反,创建一个BufferedReader并使用该readLine()方法逐行读取。使用该URLConnection.getContentType()字段找出内容的字符集(您将需要它以正确创建您的阅读器)。
Example:
例子:
URL data = new URL("http://google.com");
URLConnection dataConnection = data.openConnection();
// Find out charset, default to ISO-8859-1 if unknown
String charset = "ISO-8859-1";
String contentType = dataConnection.getContentType();
if (contentType != null) {
int pos = contentType.indexOf("charset=");
if (pos != -1) {
charset = contentType.substring(pos + "charset=".length());
}
}
// Create reader and read string data
BufferedReader r = new BufferedReader(
new InputStreamReader(dataConnection.getInputStream(), charset));
String content = "";
String line;
while ((line = r.readLine()) != null) {
content += line + "\n";
}

