-
October 10th, 2004, 04:41 PM
#1
Senior Member
End of file
Hello Guys.
I have written the code to scann display every line of the text file, but the problem i that I do not know how do we do the End Of File in Java, Na i am using "null", therefore it becomes the endless loop.
Any sort of information will be appriciated.
the following is the code.
-------------------------------------
import java.io.*;
class ReadOne
{
public static void main(String[] args)throws IOException
{
FileReader f1 = new FileReader("E:\\Harbir\\ep04ext.txt");
BufferedReader file = new BufferedReader(f1);
String text;
int i=0;
while(file.readLine()!= null)
{
i++; // counting the number of lines
text=file.readLine();
System.out.println("\n "+text);
}
System.out.println("\n The value of I is: "+i);
}
}
--------------------------------------------------------------
U get What U pay for.
-
October 10th, 2004, 04:47 PM
#2
try while(file.readLine() != -1)
I think that might be it. I thought there was a constant for EOF though somewhere.
-
October 10th, 2004, 05:05 PM
#3
in C it is:
Code:
char c;
while(c!=EOF)
i think it might be the same in java too, but i'm not sure though
-
October 10th, 2004, 05:47 PM
#4
Senior Member
I did try putting -1 in place of null, but it gives the following message during compilation.
------------------------------------------------------------
operator != cannot be applied to java.lang.String,int
-------------------------------------------------------------
I did EOF also but it say, EOF variable not recognized.
I am using Java Creator, for writting these programs.
U get What U pay for.
-
October 10th, 2004, 06:19 PM
#5
Might be overly simplistic, but it works for me:
if (text == null) break;
EDIT: A better idea would be to add a line at the end of your text file such as ENDFILE, so that when your program hits that, it ends. I know absolutely nothing about what you are trying to do, but it occurs to me that if for some reason there is a blank line between two lines of text, your program will terminate without reading in all the information from the file.
Then all you would need is:
if(text.equals("ENDFILE"), then end your application, or go on to whatever step is next.
-
October 10th, 2004, 06:33 PM
#6
Senior Member
Hello Guys, at times i really get crazy....
Actually the original code was working well, the thing is that it is actually a long file so it was taking time, and i thought that "while" loop has become endless.
Sorry for the botheration.
I really appreciate all of your help and concern.
Regards
Harbir
U get What U pay for.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|