Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,212 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,565 people online right now. Registration is fast and FREE... Join Now!




file io

 

file io, to detect end of text file

shilpi.mittal7

5 Nov, 2008 - 06:05 AM
Post #1

New D.I.C Head
*

Joined: 5 Nov, 2008
Posts: 2

java
import java.io.*;
import java.util.StringTokenizer;
public class file3
{
public static void main(String st[]) throws FileNotFoundException,IOException
{
int no;
String s,aRecord="";
StringTokenizer tokenizer;

File infile=new File("C:\\Program Files\\Java\\jdk1.5.0\\bin\\mean1.txt");
if(infile.exists())
{
FileReader aFileReader=new FileReader(infile);
BufferedReader aBufferedReader=new BufferedReader(aFileReader);
aRecord=aBufferedReader.readLine();
tokenizer=new StringTokenizer(aRecord);
System.out.println(aFileReader.read());
while(aFileReader.read()!="END")
{
s=tokenizer.nextToken();
no=Integer.parseInt(s);
System.out.println(no);
}
}
else
{
System.out.println(infile+"doesnt exist");
System.exit(0);
}

}
}




in this code,i am not able to detect end of file....my file(mean1) contains integer values and i have to print all values upto end of file...
please help me in this as i am beginner to java
thank you

User is offlineProfile CardPM
+Quote Post


gabehabe

RE: File Io

5 Nov, 2008 - 07:39 AM
Post #2

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



Thanked: 177 times
Dream Kudos: 3275
Expert In: Lots of things.

My Contributions
Moved to Java.

Also, please code.gif
User is offlineProfile CardPM
+Quote Post

pbl

RE: File Io

5 Nov, 2008 - 04:06 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
Don;t know a FileReader read method that looks like this:

CODE

while(aFileReader.read()!="END")  


an even if there is a FileReader method named read() that returns a String the test should be done that way:

CODE

while(!aFileReader.read().equals("END"))  


you cannot use == or != on Strings
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 09:47PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month