java bufferedreader - 读入 stringbuffer 而不是 string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637463/
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
bufferedreader - read into stringbuffer and not string
提问by user1958884
I have the following code. What I would like to do is read each line from the BufferedReader directly into a StringBuffer to reduce memory overhead. Once it gets to the end of the data stream I would like it to exit the while loop.
我有以下代码。我想做的是将 BufferedReader 中的每一行直接读入 StringBuffer 以减少内存开销。一旦到达数据流的末尾,我希望它退出 while 循环。
StringBuffer line = new StringBuffer();
URL url = new URL("a url");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
int count = 0;
while(line.append(reader.readLine()) != null){
System.out.println(line.toString());
line.delete(0,line.length());
}
It reads the stream fine but when I get to the end of the stream it returns null and keeps printing null without exiting the loop. Any
它可以很好地读取流,但是当我到达流的末尾时,它返回 null 并在不退出循环的情况下继续打印 null。任何
回答by MadProgrammer
This while(line.append(reader.readLine()) != null)
is basically the same as saying while(line.append(reader.readLine()).toString() != null)
which is never likely to happen.
这while(line.append(reader.readLine()) != null)
与说while(line.append(reader.readLine()).toString() != null)
永远不可能发生的说法基本相同。
The other problem you might have, is null
is actually being translated to a literal String
of "null"
. That's why it's printing "null", the value isn't actually null
- confused yet...
你可能有另一个问题,是null
实际上是被翻译成文字String
的"null"
。这就是为什么它打印“null”,该值实际上并不是null
- 还很困惑......
Instead, try something like...
相反,尝试类似...
String text = null;
while((text = reader.readLine()) != null){
line.append(text)
System.out.println(line.toString());
line.delete(0,line.length());
}
Updated
更新
While I'm here, I might suggest that you are actually not saving your self anything.
当我在这里时,我可能会建议您实际上并没有为自己节省任何东西。
readLine
will create String
object, which you're putting into a StringBuffer
. You're not actually saving any memory, but rather complicating the process.
readLine
将创建String
对象,您将其放入StringBuffer
. 您实际上并没有节省任何内存,而是使过程复杂化。
If you're really worried about creating lots of String
objects in memory, then use BufferedReader#read(char[])
instead. Append the resulting character array to the StringBuffer
.
如果您真的担心String
在内存中创建大量对象,请BufferedReader#read(char[])
改用。将生成的字符数组附加到StringBuffer
.
Also, unless you need synchronized access to the StringBuffer
, use StringBuilder
instead, it's faster.
此外,除非您需要同步访问 , 否则StringBuffer
使用StringBuilder
它会更快。
回答by user1958884
This works perfectly. You just have to catch the NUllPointerException
这完美地工作。你只需要捕捉 NUllPointerException
while(line.append(reader.readLine().toString()) != null){
回答by mfaerevaag
You could try the same with this for-loop:
你可以用这个 for 循环尝试同样的方法:
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line); // Or whatever
}