Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可序列化简单对象IOException_Java_Android_Ioexception_Serializable_Objectoutputstream - Fatal编程技术网

Java 可序列化简单对象IOException

Java 可序列化简单对象IOException,java,android,ioexception,serializable,objectoutputstream,Java,Android,Ioexception,Serializable,Objectoutputstream,找到了解决方案: 你必须像这样打开溪流: FileInputStream inputStream = openFileInput(FILENAME); ObjectInputStream ois = new ObjectInputStream(inputStream); FileInputStream inputStream = openFileInput(FILENAME); ObjectInputStream ois = new ObjectInputStream(inputStrea

找到了解决方案: 你必须像这样打开溪流:

FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);
FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);
输出也一样。如果有人在寻找答案时偶然发现这一点,这就为我解决了问题

原始问题: 通过对
Toast
s的一些测试,我发现当我调用
ObjectOutputStream
的构造函数时,抛出了一个
IOException

我的代码看起来像这样。请注意,这只是一个测试项目,我甚至不能让这个项目工作

    Button b = new Button(this);
    b.setText("write");
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            try {
                File f = new File("Filepath");
                if (!f.exists()) {
                    f.createNewFile();
                }

                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(f)); //IOException here!

                Series x = new Series("Test", 20, 12);
                // oos.writeObject(x);

                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    tv = new TextView(this);
    tv.setText("Not read anything yet!");

    Button r = new Button(this);
    r.setText("Read");
    r.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                ObjectInputStream ois = new ObjectInputStream(
                        new FileInputStream(new File("Filepath")));
                Series y = (Series) ois.readObject();
                tv.setText(y.getName() + "-" + y.getNumOfSeason() + "-"
                        + y.getNumOfEpisode());
                ois.close();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
问题似乎是我的构造函数调用。在我添加带有

if (!f.exists()) {
                f.createNewFile();
            }
我得到了一个
FileNotFoundException


我做错了什么?

当程序在磁盘中或您试图访问该文件的任何地方找不到该文件时,会引发FileNotFoundException。
请检查您的文件路径并重试。

我不确定这一点,但我认为您可以尝试使用格式化程序,如果文件不存在,格式化程序将创建该文件

Formatter formatter = new Formatter (file);

我希望它能有所帮助。

这里是Oracle文档的一个摘录,供您参考

如果文件存在,但它是目录而不是常规文件,则 不存在但无法创建,或无法为任何其他文件打开 然后引发FileNotFoundException


在您的情况下,我认为该文件被视为一个目录,因为它没有扩展名,因此引发了异常。

找到了解决方案: 你必须像这样打开溪流:

FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);
FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);

输出也一样。这为我解决了这个问题,如果有人在寻找答案时偶然发现它。

我通过检查文件是否存在以及是否没有创建它来避免出现
FileNotFoundException
。我在OOS的构造函数调用中得到了一个IOException。同样,您正在避免FileNotFoundExecution,但之后会得到IOException。你确定你的文件路径吗?我只是放了一个字符串,而不是写着“test”的文件路径。仍在获取FileNotFoundException。是否应该在我打开流时创建它?使用真实的文件进行测试,例如,将它放在C:/中,并让文件构造函数通过文件f=new file(“C:/yourFile.txt”)引用它;我正在我的手机上测试这个。仅仅使用
字符串
文件
构造函数不就可以了吗?我添加了一个扩展名,仍然会抛出FNFE。@AdrianJandl,我只是在做假设,你必须根据文档中提到的案例来调查你的问题。您确定没有任何东西阻止创建或打开文件吗?