If you want to know how many bytes are in the file, then File.length() gives you that.
If you want to write at the end of the file, then FileWriter and FileOutputStream both have constructors that allow you to ask to append to the file. Similarly, you can seek() to the end of a RandomAccessFile, then write to it.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
And if you're reading a file, the input stream or reader will tell you when you've hit the end. This kind of thing is very common for reading text files one line at a time:
See how the loop stops - or never starts - when the file is at end?
Actually it's more common to tighten this up a bit with some nifty Java syntax:
The assignment clause "line = reader.readLine()" not only loads the variable line, but it's also an expression with a return value, the same variable. So we can set line and test it for null all in one shot.
Streams work a differently - they report the number of bytes actually read by a read() method, and 0 means end of file.
Does that work for you?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
[Shubhada]: is there any key by which we can end a file after writing?
Generally you just need to close() the FileWriter or FileOutputStream or whatever you're writing the file with, when you're done writing. That's all.
"I'm not back." - Bill Harding, Twister
Joined: Jan 30, 2000
[Stan]: Streams work a differently - they report the number of bytes actually read by a read() method, and 0 means end of file.
End of file is indicated by -1. This is used for both InputStreams and Readers, at least for the read() methods. BufferedReader's readLine() returns a String which may indicate EOF with a null, but most of the other read methods return an int which may be -1 to indicate EOF.