• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

Having homework problem on throws File IOException

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
daskino12
Asked:
daskino12
  • 6
  • 4
1 Solution
 
jimmackCommented:
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
 
jimmackCommented:
What am I talking about "FileIOException"  that should read "IOException" ;-)
0
 
daskino12Author Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jimmackCommented:
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
 
daskino12Author Commented:
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
 
jimmackCommented:
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
 
daskino12Author Commented:
Hello,

Cool.  Thanks a lot.

0
 
jimmackCommented:
;-)
0
 
jimmackCommented:
Have you got enough to "accept" this or are you waiting to see if there's some more?
;-)
0
 
daskino12Author Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now