Can BufferedReader continue to read line in text file when pass to another function?

那年仲夏 提交于 2020-01-02 18:57:09

问题


I have this function to read text file with BufferedReader. I want to know if I can called another function and pass the readline/continue to read text file. If it can pass, can it loop the same function?


回答1:


A BufferedReader object is self-contained (thanks to Java's heavy emphasis on Object Orientation), so you are free to pass it between functions and inside loops, and it will retain it's state - including the read line.

However, if you are trying to call a method to process the line you have just read, then it would be much more efficient to pass the line itself to the method as a String, instead of passing the entire BufferedReader object.

while( myBufferedReader.ready() ) {
    String line = myBufferedReader.readLine();
    processLine( line );
}


来源:https://stackoverflow.com/questions/10629269/can-bufferedreader-continue-to-read-line-in-text-file-when-pass-to-another-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!