Solved

Having homework problem on throws File IOException

Posted on 2003-11-01
10
381 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
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
What am I talking about "FileIOException"  that should read "IOException" ;-)
0
 

Author Comment

by:daskino12
Comment Utility
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
 
LVL 15

Accepted Solution

by:
jimmack earned 250 total points
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
Hello,

Cool.  Thanks a lot.

0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
;-)
0
 
LVL 15

Expert Comment

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

Author Comment

by:daskino12
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 70
count11 challenge 6 47
Groovy:unable to resolve class error 2 27
Java / Linux and Regular Expressions 11 42
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now