Restriction on readLine()
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.
When buffered reader is used, the entire file is never read into memory, so it should be able to handle files of any size that your operating system supports for.
It can read any number of lines .
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) {
}
精彩评论