Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

java - What does it really mean by reading file into memory all at once?

I am confused with the term "Do not read file into memory all at once". For the below code, will it be correct to say that I not reading file into memory all at once but line by line?

File file = new File("C:\Users\Desktop\test.txt"); 
  
  BufferedReader br = new BufferedReader(new FileReader(file)); 
  
  String st; 
  while ((st = br.readLine()) != null) 
    System.out.println(st); 
  } 
} 
question from:https://stackoverflow.com/questions/65837816/what-does-it-really-mean-by-reading-file-into-memory-all-at-once

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For the below code, will it be correct to say that I not reading file into memory all at once but line by line?

You cannot make any such assumptions. Since you are using a BufferedReader, all you can say with assuredness is that you are reading some of the file into a memory buffer, perhaps all of the file, but the buffer is in control, not you. And when the buffer's data has been depleted, then more of the file will be read into it, if there is still data remaining to be read.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...