readChar() method and EOF exception

Posted on 2002-03-21
Last Modified: 2010-05-18
I am a CS major and am trying to improve on my programming skills, but I have come across a roadblock. I am using the readChar() method to read characters one by one from a specified file and almost everything is going smoothly except that I am not sure on the most efficient way to let my program know it has reached the end of a file. I am trying to use the End Of File execption, but it still reads on forever. I pasted my code here for an illustration. Any pointers?

- Alex


public class FileDemo {
    public static void main(String[] args) throws IOException {

     if (args.length < 1){
         System.out.println("Program syntax: java FileDemo FILE.XXX");

     DataInputStream in = new DataInputStream(
                          new FileInputStream(args[0]));

     InputStreamReader isr = new InputStreamReader(in);

     System.out.println("<<Start of file>>");

     try {
         char newChar = (char);
         while(true) {
          newChar = (char);
     } catch (EOFException e) { }
     System.out.println("<<End of file>>");    
Question by:alrav79
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Accepted Solution

m_onkey_boy earned 50 total points
ID: 6887452
int c;
while ((c = != -1) {

Expert Comment

ID: 6887462
BTW - in your example, DataInputStream is not adding anything to your program other than overhead.

Author Comment

ID: 6887479
what do you mean overhead?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6887507
could have explained the process a litte more

Expert Comment

ID: 6888103
You didn't give me a chance to elaborate - you could have asked.


What I meant by overhead was that by using the datainputstream, you have instantiated an object tha was not needed.  Also, each call to read() is being passed up an extra method call ( calls, which in turn calls  Why not cut out the middleman?

As far as explaining the process - the code is self-explanatory.  I guess the part you should notice is that -1 is the EOF marker in a Java stream.

Author Comment

ID: 6888508
sorry, man, that was my fault....thanks for the explanation though; it was very helpful

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 58
spring maven example issues 3 109
Website checklist for browser compatibility? 2 57
Netbeans and org.apache.commons.lang3 issue 3 55
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question