views:

64

answers:

5

Hello All,

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.

+2  A: 

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.

johnbk
Although I don't think BufferedReader depends on your Operating System, since only the buffer is read at a time.
Valchris
@Valchris, I was trying to say you can read any number of lines from a file of maximum size that a particular os allows.
johnbk
@johnbk, If your OS restricts you to files < 4GB and you have a file that's > then that, via a Duel boot, or hard drive swapping, you should still be able to load it via the buffer. Java circumvents that OS limitation.
Valchris
@Valchris: that's nonsense - Java has to work with the APIs that the OS provides. However, file size limits are generally not OS-specific but filesystem specific.
Michael Borgwardt
A: 

It can read any number of lines .

giri
A: 

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) {  
}  
Nik
Can't figure how to indent the code...
Nik
I did it for you. You may find the the button labeled 010 in the edit window's toolbar useful. (Shortcut: Ctrl-K)
meriton
Why do you have two checks if line is not null? I like the single while loop better.
Marc
I agree. I wouldn't consider this idiomatic.
dty
A: 

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
} 
Michael McGowan
A: 

Thanks for the replies

collegian