简体   繁体   English

Java FileInputStream和FileOutputStream错误

[英]Error with java FileInputStream and FileOutputStream

I just made my first I/O based stuff in Java. 我刚刚用Java编写了第一个基于I / O的东西。 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? 有人有解决问题的想法吗?

Take a look at http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html#readObject() . 看看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. 对于列出的不是RuntimeException的子类的每个异常,您都需要捕获该异常或声明该方法可以引发该异常。 You have only done this for IOException . 您仅针对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. 对于引发非运行时异常的所有方法,都需要这样做。

Your IDE is letting you run some code even though you are missing some classes or despite having compilation errors. 即使缺少某些类或存在编译错误,IDE仍允许您运行一些代码。 Fix the compilation errors before you run them. 运行编译错误之前,请先对其进行修复。

Your code is uncompilable at the moment. 您的代码目前无法编译。 Line 36 fails. 第36行失败。

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

This is because the class check does not contain a method getType() . 这是因为类检查不包含方法getType() Maybe You meant something along the lines of getClass().getName() ? 也许您的意思与getClass().getName()

Fix this error and try again. 解决此错误,然后重试。 Your own error message does not make sense to me - is it generated by an IDE? 您自己的错误消息对我来说没有意义-它是由IDE生成的吗?

PS. PS。 Have a look at Java coding conventions regarding the naming of classes, variables and such. 看一下有关类,变量等命名的Java编码约定 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM