Why this code throws 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(); } } }
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)




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