I just wanted to know if there is any restriction on the number of lines readLine method can read from a file in java.Any help will be grately appreciated.This is what I am talking about:
FileReader fr1=new FileReader("/homes/output_train_2000.txt");
BufferedReader br1=new BufferedReader(fr1);
while((line1=br1.readLine())!=null){ }
Thanks.
使用缓冲读取器时,整个文件永远不会读入内存,因此它应该能够处理操作系统支持的任何大小的文件。
它可以读取任意多行。
Are you trying to restrict the number of lines read? If so then you can easily add some code to do that:
FileReader fr1=new FileReader("/homes/output_train_2000.txt");
BufferedReader br1=new BufferedReader(fr1);
int numLinesRead = 0;
int maxLines = 1000;
while((numLinesRead < maxLines) && (line1=br1.readLine())!=null){
numLinesRead++;
// other stuff
}
No restriction that I know of. Here's a better way of doing it:
BufferedReader reader = null;
try {
reader = new BufferedReader( new FileReader( "/homes/output_train_2000.txt") );
String line = null;
do {
line = reader.readLine();
if( line != null ) {
// Do something
}
} while( line != null );
} catch (Exception e) {
e.printStackTrace();
} finally {
if( reader != null )
try {
reader.close();
} catch (IOException e) {
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.