Results 1 to 8 of 8
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why am I getting an EOF Exception?

    I have a file called Human.java and it has 3 three lines of text with 5 characters each.

    Here is my code:

    Java Code:
    public class TestClass {
    
    	public static void main(String[] args) throws IOException {
    		BufferedInputStream test = new BufferedInputStream(new FileInputStream(new File("c:/Human.java")));
    		System.out.println(test.read());
    		System.out.println(test.read());
    		DataInputStream den = new DataInputStream(test);
    		System.out.println(test.read());
    		System.out.println(den.readUTF());
    	}
    }
    And here is the output:

    107
    111
    114
    Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readFully(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at myPackage.TestClass.main(TestClass.java:19)
    What is wrong with my DataInputStream please ?

    Thank you.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting an EOF Exception?

    Don't read both through the FileInputStream and the DataInputStream; only read through the DataInputStream (it wraps the other stream).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why am I getting an EOF Exception?

    Oh so it is like open in both streams thats why ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting an EOF Exception?

    Quote Originally Posted by fatabass View Post
    Oh so it is like open in both streams thats why ?
    Not really; the wrapped stream is a buffered stream and when you wrap it later (after it has read something already) it may have read ahead and the wrapper stream might only 'see' the last portion of the stream.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why am I getting an EOF Exception?

    Interesting.. I would like to learn the reason in detail. Any sources you can suggest ?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting an EOF Exception?

    Quote Originally Posted by fatabass View Post
    Interesting.. I would like to learn the reason in detail. Any sources you can suggest ?
    The API documentation for the entre java.io package will do fine; it shows how all the InputStreams are implemented as 'decorators' or 'wrappers'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why am I getting an EOF Exception?

    When you say the documentation, do you mean: java.io (Java Platform SE 6)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why am I getting an EOF Exception?

    Quote Originally Posted by fatabass View Post
    When you say the documentation, do you mean: java.io (Java Platform SE 6)
    Yep, that's the java.io part of the API documentation (for version 1.6); bookmark the entire API documentation, you'll need it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  4. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •