Solved

Having homework problem on throws File IOException

Posted on 2003-11-01
10
396 Views
Last Modified: 2010-03-31
Hello,

I am doing a homework with the following exception:

public static String readOneString( String filename)
    throws File IOException

Can anyone please give me some suggestions?

Thank
0
Comment
Question by:daskino12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9664326
You'll need to provide more detail.

First look at the javadocs for FileIOException to see if that helps.

Can you post the entire exception?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9664333
What am I talking about "FileIOException"  that should read "IOException" ;-)
0
 

Author Comment

by:daskino12
ID: 9664338
Hello,

I am sorry about that.  Well, the assignment is about using a programmer-defined excerption type that can be more helpful for localizing a problem in a designated set of I/O operations.

Define a new exception by

  class FileIOException extends Exception {}

to be used for a new Java class, FilIO with the static methods listed below.  Each method should re-throw any exceptions of the form IOException as a FilIOException.

class fileIO {

      public static void main (String[] args) throws Exception

      public static String readOneString(String filename)  
        throws FileIOException

      public static int readOneInt(String filename)
        throws FileIOException

      public static double readOneDouble(String filename)
        throws FileIOException
      
      public static String[] readAllStrings(String filename)
        throws FileIOException

      public static int[] readAllInts(String filename)
        throws FileIOException
      
      public static double[] readAllDoubles(String filename)
        throws FileIOException

      public static void writeOneString(String data, String filename)
        throws FileIOException

      public static void writeOneInt(int data, String filename)
        throws FileIOException

      public static void writeOneDouble(double data, String filename)
        throws FileIOException

      public static void writeAllStrings(String[] strArray, String filename)
        throws FileIOException
      
      public static void writeAllInts(int[] intArray, String filename)
        throws FileIOException

      public static void writeAllDoubles(double[] doubleArray, String filename)
        throws FileIOException

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
jimmack earned 250 total points
ID: 9664360
Err... OK.

In each of your methods, you'll need to do the following:

try
{
    //  The appropriate file read/write
}
catch (IOException ioe)
{
    throw new FileIOException("Some descriptive text " + // any info you want to extract from the IOException
}

You'll also need your FileIOException class

public class FileIOException extends Exception
{
    public FileIOException()  // Overrides the empty constructor
    {
        super();
        .
        .
    }

    public FileIOException(String s)
    {
        super(s);
        .
        .
    }
}
0
 

Author Comment

by:daskino12
ID: 9664374
Hello,

Cool.  Thanks.  I was just wondering..can you also please give me some suggestions on how to read one string (just one data item from a file)?

Thanks
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9664385
I'm sure you realise that we can't actually do your homework for you ;-)

Bearing that in mind and taking your class as a guide for what you really want to do, this is how you would normally read a String (UTF String) from a data file (not a text file):

FileInputStream fis = new FileInputStream("filename.txt");
DataInputStream dis = new DataInputStream(fis);

try
{
    String oneLine = dis.readUTF();
}
catch (IOException ioe)
{
    // handle the exception, eg.
    System.err.println("IOE: " + ioe.toString());
}

You should be able to adapt that for your needs.  I think any more might be a bit too much ;-)

Good luck.
0
 

Author Comment

by:daskino12
ID: 9664393
Hello,

Cool.  Thanks a lot.

0
 
LVL 15

Expert Comment

by:jimmack
ID: 9664398
;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9664426
Have you got enough to "accept" this or are you waiting to see if there's some more?
;-)
0
 

Author Comment

by:daskino12
ID: 9664754
Hello,

I am sorry..i will accept it...thanks a lot.

You have helped a lot..i am just still trying to figure it out...:)

Thanks
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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