SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: EOFException

  1. #1
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    EOFException

    Code:
    package fiedata;
    
    import java.io.*;
    
    public class Main {
    
        static final int[] prices = { 4500, 3000, 2500 };
        static final String[] books = { "JAVA","PHP","Mysql"};
        public static void main(String[] args) throws IOException {
        
    
          DataOutputStream out=null;
          out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("books.dat")));
    
          for (int i = 0; i < prices.length; i ++) {
    
               out.writeInt(prices[i]);
               out.writeUTF(books[i]);
               out.flush();
              }
    
          DataInputStream in=null;
          in = new DataInputStream(new BufferedInputStream(new FileInputStream("books.dat")));
          int price;
          String book;
    
          try {
              
              while (true) {
    
                         price = in.readInt();
                         book= in.readUTF();
                         System.out.format("saved in file %s  %d%n",book, price); }
           }
          catch (EOFException e) {
    
                    e.printStackTrace();
          }
    
        }
       }
    Why this code throws EOFException?


    saved in file JAVA 4500
    java.io.EOFException
    saved in file PHP 3000
    saved in file Mysql 2500
    at java.io.DataInputStream.readInt(DataInputStream.java:375)
    at fiedata.Main.main(Main.java:41)
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Really? You wrote an infinite loop to read your finite file and can't understand why you're getting an EOFException?

  3. #3
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Well, In fact, I saw that loop in sun site:

    http://download.oracle.com/javase/tu...tastreams.html

    Then I must report this mistake to sun yes?

    and of course, Nice answer, Thank you! Happy Christmas!
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  4. #4
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Directly after the bit of code you copied the text goes on to say:
    Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.
    So, no, it's not Sun's fault that you didn't bother to read the explanation of the code.

    And, before you go running off to use exceptions for flow control, know that doing such is considered to be poor programming, due to the relatively massive overhead of generating an exception verses simply testing conditions. Had they a choice in the case of DataInput, et al, they would not have done that way. (Feel free to contact Sun and suggest what return value could be used to indicate EOF when any value may be valid and expected)

  5. #5
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I change the code

    Code:
    package fiedata;
    
    import java.io.*;
    
    /**
     *
     * @author Administrator
     */
    public class Main {
    
        static final int[] prices = { 4500, 3000, 2500 };
        static final String[] books = { "JAVA","PHP","Mysql"};
        public static void main(String[] args) throws IOException {
            // TODO code application logic here
                 
    
          DataOutputStream out=null;
          out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("books.dat")));
    
          for (int i = 0; i < prices.length; i ++) {
    
               out.writeInt(prices[i]);
               out.writeUTF(books[i]);
               out.flush();
              }
    
          DataInputStream in=null;
          in = new DataInputStream(new BufferedInputStream(new FileInputStream("books.dat")));
          int price;
          String book;
    
          try {
              //boolean eof = false;
              while (in.available() != 0) {
                  //if (!in.ready()) break;
    
                         price = in.readInt();
                         book= in.readUTF();
                         System.out.format(" %s  %d%n",book, price);
                        
                         //if (in == -1)
                              //eof = true;
    
              }
           }
          catch (EOFException e) {
    
                    e.printStackTrace();
          }
    
        }
       }
    What is your idea?
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  6. #6
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    What is my idea for what? The code works fine.

Bookmarks

Posting Permissions

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