Solved

readChar() method and EOF exception

Posted on 2002-03-21
6
366 Views
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


import java.io.*;

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

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

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

     InputStreamReader isr = new InputStreamReader(in);


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


     try {
         char newChar = (char) isr.read();
         while(true) {
          System.out.print(newChar);
          newChar = (char) isr.read();
         }
     } catch (EOFException e) { }
     in.close();
     
     System.out.println("<<End of file>>");    
    }
}
0
Comment
Question by:alrav79
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 50 total points
ID: 6887452
int c;
while ((c = isr.read()) != -1) {
  System.out.println((char)c);
}
0
 
LVL 4

Expert Comment

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

Author Comment

by:alrav79
ID: 6887479
what do you mean overhead?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:alrav79
ID: 6887507
could have explained the process a litte more
0
 
LVL 4

Expert Comment

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

OK:

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 (InputStreamReader.read() calls DataInputStream.read(), which in turn calls FileInputStream.read()).  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.
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 18
Java asynchronous logging 4 49
how do i compare an object based on two fields 6 48
javap bin 2 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now