java 一次性读取一个 inputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5751510/
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
Reading an inputStream all at once
提问by Sujit Agarwal
I have developed a j2me application that connects to my webhosting server through sockets. I read responses from the server using my own extended lineReader class that extends the basic InputStreamReader. If the server sends 5 lines of replies, the syntax to read the server replies line by line is:
我开发了一个 j2me 应用程序,它通过套接字连接到我的虚拟主机服务器。我使用扩展了基本 InputStreamReader 的我自己的扩展 lineReader 类从服务器读取响应。如果服务器发送 5 行回复,则逐行读取服务器回复的语法为:
line=input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
In this case, i can write this syntax because i know that there is a fixed number of replies. But if I dont know the number of lines, and want to read the whole inputStream at once, how should I modify the current readLine()
function. Here's the code for the function:
在这种情况下,我可以编写此语法,因为我知道有固定数量的回复。但是如果我不知道行数,并且想一次读取整个inputStream,我应该如何修改当前readLine()
函数。这是该函数的代码:
public String readLine() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
sb.append((char)c);
}
//By now, buf is empty.
if (c == '\r') {
//Dos, or Mac line ending?
c = super.read();
if (c != '\n' && c != -1) {
//Push it back into the 'buffer'
buf = (char) c;
readAhead = true;
}
}
return sb.toString();
}
采纳答案by Andrew White
What about Apache Commons IOUtils.readLines()?
Apache Commons IOUtils.readLines()怎么样?
Get the contents of an InputStream as a list of Strings, one entry per line, using the default character encoding of the platform.
使用平台的默认字符编码以字符串列表的形式获取 InputStream 的内容,每行一个条目。
Or if you just want a single string use IOUtiles.toString().
或者,如果您只想要一个字符串,请使用IOUtiles.toString()。
Get the contents of an InputStream as a String using the default character encoding of the platform.
使用平台的默认字符编码以字符串形式获取 InputStream 的内容。
[update] Per the comment about this being avaible on J2ME, I admit I missed that condition however, the IOUtils sourceis pretty light on dependencies, so perhaps the code could be used directly.
[更新] 根据 J2ME 上关于此可用的评论,我承认我错过了那个条件,但是IOUtils 源在依赖项上非常轻,所以也许可以直接使用代码。
回答by MByD
If I understand you correctly, You can use a simple loop:
如果我理解正确,您可以使用一个简单的循环:
StringBuffer sb = new StringBuffer();
String s;
while ((s = input.readLine()) != null)
sb.append(s);
Add a counter in your loop, and if your counter = 0, return null:
在循环中添加一个计数器,如果您的计数器 = 0,则返回 null:
int counter = 0;
while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
sb.append((char)c);
counter++;
}
if (counter == 0)
return null;
回答by Vikrant Goel
Specifically for web server !
专用于网络服务器!
String temp;
StringBuffer sb = new StringBuffer();
while (!(temp = input.readLine()).equals("")){
sb.append(line);
}