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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:55:21  来源:igfitidea点击:

bufferedreader - read into stringbuffer and not string

javastreamwhile-loopbufferedreaderstringbuffer

提问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 nullis actually being translated to a literal Stringof "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.

当我在这里时,我可能会建议您实际上并没有为自己节省任何东西。

readLinewill create Stringobject, 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 Stringobjects 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 StringBuilderinstead, 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
}