Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Exception Handling Code Doesn't Work

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The following is from Chapter 5, Sierra/Bates



I created myfile.txt in the same directory, and now I receive the following error:

IO Error
java.io.EOFException
java.io.EOFException
at java.io.RandomAccessFile.readFully(RandomAccessFile.java:383)
at ReadData.main(ReadData.java:8)



Two questions:
1. How do I fix this error?
2. Why does FileNotFoundException handler say, System.err.println(e.getMessage()), while IOException handler says System.err.println(e.toString());?
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Sandra

Would you try using following code at line number 9 and if you try then post output of code

I think file should have minimum 1000 bytes to read other wise EOF will be thrown.
see this
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Sandra.

1. How do I fix this error?



Increase the size of 'myfile.txt' file upto 1000 bytes. I think this will fix the error.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chinmaya Chowdary wrote:Increase the size of 'myfile.txt' file upto 1000 bytes. I think this will fix the error.


It won't solve the problem. Ninad is correct. Since Sandra is using readFully the call throws exception if the file ends before reading 1000 bytes. Instead of readFully use read(byte[]) method...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I replaced readFully with read and now the program compiles and runs.

Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic