• Post Reply Bookmark Topic Watch Topic
  • New Topic

EOF in FileInputStreams  RSS feed

 
Mazhar Shaikh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I need a small help from you people.
My question is
"How will one check for the End Of File condition in FileInputStream ?"
Details:::
I have stored some String objects in a file say xyz.tmp by using the function writeObject().So while reading the file now i need to know the EOF(of this FileInputStream) so that i can use while LOOP and read the Objects into an arrayList.I hope you must have got the problem.
Thanx
Mazhar
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


FileInputStream has a method called available() that tells you how many bytes there are in a file. So, you culd do the following:


FileInputStream fin = new FileInputStream("xyz.tmp");
for(int i = 0; i < fin.available(); ++i) {
...
}


Cheers,


RL
 
Mazhar Shaikh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx RL,
for your great reply.
But still the proplem persists.
Actually i am storing String Objects into this file(xyz.tmp)ok.
Now this fin.Available() will return bytes ,write.
According to your for loop i'll have to read bytes for each iterations.But i need to read String Objects,which is a collection of bytes.Everytime time i'll have to skip some(length og String object) bytes to come to the next String Object.
Well i am a beginner,so i don't have much idea.Plz let me know.
Thanx
Mazhar
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I see your problem a little more clearly now. Since you will be reading in an object from the stream, you can simply just check to see if the object is null.. If it is, then you have reached the end of the file.. Here is a quick example:
<pre>
import java.io.*;
public class ReadFile {
public static void main(String[] args) throws IOException {
FileWriter fout = new FileWriter("fubar.txt");
PrintWriter pw = new PrintWriter(fout);
pw.println("Here");
pw.println("is");
pw.println("some");
pw.println("string");
pw.println("text.");
pw.close();
fout.close();
FileReader fin = new FileReader("fubar.txt");
BufferedReader br = new BufferedReader(fin);
String s = br.readLine();
while(s != null) {
System.out.println(s);
s = br.readLine();
}
br.close();
fin.close();
}
}
</pre>
Hope this will help you solve your problem!
Cheers,
RL
[This message has been edited by Ryan Langley (edited May 23, 2001).]
[This message has been edited by Ryan Langley (edited May 23, 2001).]
 
Mazhar Shaikh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there RL,
Thanx thanx thanx a lot.
Really i could not imagined such simple lines code.
Thanx for your firm guidance.
I tried it and included into my project.
Thanx a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!