end of file \ file offset
hi,
i am using BufferReader to read lines from a text file.
i would like to create a boolean method names hasMore() which will be indicating if the end-of-file reached.
here is a pesudo
if(hasMore())
line = br.readLine()
and speaking of files, how do i get the offset within the open file which i've been reading its lines?
Re: end of file \ file offset
Not sure why you wish to create the new method, all you really need to do is check if the readLine() method returns null.
Code :
String line;
while ( ( line = br.readLine() ) != null ){
//line contains the line contents of the line just read
}
As for offset, don't think there is a way to do this directly through the reader object but you can tally what has been read so far and use this as the offset.
Re: end of file \ file offset
If your buffer supports marking (I believe file buffers do), you can "dummy read" one character and then go back to your marked spot.
Code :
public boolean hasMore(BufferedReader reader) throws IllegalArgumentException
{
if (reader.markSupported())
{
reader.mark(1);
int val = reader.read();
reader.reset();
if (val == -1)
{
return false;
}
else
{
return true;
}
}
else
{
// marking not supported
throws new IllegalArgumentException();
}
}
Re: end of file \ file offset
first of all, thanks.
i will try it now.
now i have this to handle...
i am opening a file using BufferedReader and march through the file once. after it, i have to return to the beginning of the file, and i would like to acheive it without closeing the file and reopen it.
how can it be done?
i am still wondering how come java became more complicated then c language... :(
Re: end of file \ file offset
Quote:
Originally Posted by
JayRay
now i have this to handle...
i am opening a file using BufferedReader and march through the file once. after it, i have to return to the beginning of the file, and i would like to acheive it without closeing the file and reopen it.
how can it be done? :(
Use the method Helloworld suggested...mark the bufferedreader when you open the file, read it, then call reset on the reader...have a look at these methods in the api of BufferedReader to learn specifically how to use them. You must pass a value to mark that indicates how far it expects to read while still preserving the mark
Quote:
i am still wondering how come java became more complicated then c language... :(
I'm sure there are just as many others out there asking the opposite question
Re: end of file \ file offset
Quote:
i am still wondering how come java became more complicated then c language...
It's not, in Java you don't have to manage your memory manually, or worry about "cryptic" symbols, or linking, or multiple inheritance issues, or operator overloading, or pointers, or tons of OS-specific problems/compiler specific problems... etc. etc. :P
Also, if you're reading through a whole file, I would strongly suggest against marking, especially for larger files. Marking basically creates a separate buffer that has the set amount of data you specify put in. This leads to two issues (well, there might be more):
1. You have to know how big of a buffer you need. If you have access to the original file reference, this won't be too difficult of a task.
2. Because you are creating a separate buffer, you're wasting a ton of memory just to store the file in memory twice.
In java (and actually, in C/C++, too), it's not an uncommon practice to read through a whole file, closing it, and the re-opening that file for read again.
If you want to access any part of a file at any time, I'd suggest looking at the Random Access File Class.