Juzz Coding
Juzz Coding

Reputation: 321

Error with java FileInputStream and FileOutputStream

I just made my first I/O based stuff in Java. I want to check if the content written to a file is properly saved in, or not. For which i wrote the following code..

    import java.io.*;

    public class check implements Serializable {
        //Make two variables and methods to initialise them
        private int height;
        private int width;
        public void setWidth(int w){
            width = w;
        }
        public void setHeight(int h){
            height = h;
        }
        public static void main(String[] args) {

        check obj = new check();
        obj.setHeight(20);
        obj.setWidth(30);

    try{
        FileOutputStream fs = new FileOutputStream("foo.txt");
        ObjectOutputStream os = new ObjectOutputStream(fs);
        os.writeObject(obj);
        os.close();
    }
    catch(IOException ex){
    }
    //We set them to null so we can't access the objects on heap.
      obj = null;  

    //Now we read them back from file   
    try{
        ObjectInputStream is = new ObjectInputStream(new FileInputStream("foo.txt")); 
        check stored = (check) is.readObject();

    //Check to see if it worked.
     System.out.println("Variable, stored contains.." + stored.getType());
    }
    catch(IOException ex){
        }

    }
 }

But it produces the following error.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    at check.Check.main(Check.java:33)

Anyone got any idea to solve the issue?

Upvotes: 2

Views: 433

Answers (3)

Viktor Seifert
Viktor Seifert

Reputation: 636

Take a look at http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html#readObject(). The method lists a couple of exceptions. For every exception listed that is not a sub-class of RuntimeException you need to either catch the exception or declare that the method can throw that exception. You have only done this for IOException. You also need to do this for the other exceptions listed in the documentation. This needs to be done for all methods that throw non-runtime exceptions.

Upvotes: 1

Tarmo R
Tarmo R

Reputation: 1153

Your code is uncompilable at the moment. Line 36 fails.

System.out.println("Variable, stored contains.." + stored.getType());

This is because the class check does not contain a method getType(). Maybe You meant something along the lines of getClass().getName()?

Fix this error and try again. Your own error message does not make sense to me - is it generated by an IDE?

PS. Have a look at Java coding conventions regarding the naming of classes, variables and such. :)

Upvotes: 0

Saj
Saj

Reputation: 18702

Your IDE is letting you run some code even though you are missing some classes or despite having compilation errors. Fix the compilation errors before you run them.

Upvotes: 0

Related Questions