• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

EOF in Java, does it exist???

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I wonder if anyone can help me on this EOF matter. Is there a general EOF check? I've read some other people's replies saying that there is a different check for different reading methods. I've got a test function below which just prints the output of the file on the screen (one char at a time, without spaces), but after it passes the actual EOF, it starts printing "???" endlessly. Don't worry about the list return, that's the continuation of the program.
Thnak you in advance for your help!
Phil

static List convert(BufferedReader input)
{
Vector searchVector = new Vector();
char charIn = '\0'; // dummy value

String word = "dummy value";
while (charIn != -1)
{
try
{
charIn = (char) input.read();
}
catch (IOException err)
{
System.out.println("Error reading file");
System.exit(1);
}
while (charIn != ' ' && charIn != -1)
{
boolean once = true;
if(word.equals("dummy value"))
{
word = new Character(charIn).toString();
break;
}
else
{
word = word + charIn;
System.out.println("FINAL WORDS: " + word);
break;
}
}
searchVector.add(word);
}
String[] searchArray = new String[searchVector.size()];
searchVector.copyInto(searchArray);
List lst = new Vector();
for (int i = 0; i<searchArray.length; i++)>
{
lst.add(searchArray[i]);
}
return lst;
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can never get EOF to compile. But---
while (reader != null) usually works.
So null instead of EOF.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileInputStream fis=new FileInputStream("c:\\a.txt");
generally, a null checking or checking for a return of -1 when read from the file, both works.
int ch=0;
while( (ch=fis.read())!=-1 )
{
System.out.print((char)ch);
}
fis.close();

HTH,
prasad.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic