java.io.FileNotFoundExeption问题

时间:2015-09-16 00:13:15

标签: java serialization objectinputstream objectoutputstream

我正在构建的程序存在问题,该程序将测试结果保存到学校本地驱动器上的文件中。问题是,当我尝试将测试结果保存到我使用JFileChooser选择的文件时,程序会抛出java.io.FileNotFoundException。以下是将测试结果序列化为文件的方法代码。

@Override
        public void testEnded() {
            start.setEnabled(true);
            pause.setEnabled(false);
            end.setEnabled(false);
            results.setVisible(false);
            panel.setVisible(false);
            cheatSheet.setEnabled(true);
            tableTest.setEnabled(true);
            TestEntryEvent event = new TestEntryEvent(name, panel.getCorrectCount(), panel.getIncorrectCount());

            if(panel.getTotalCount() == 158) {
                LinkedList<TestEntry> entries = new LinkedList<TestEntry>();
                TestEntry entry = new TestEntry(event.getName(), event.getCorrect(), event.getIncorrect());
                ObjectInputStream ois = null;
                File file;

                if(JOptionPane.showConfirmDialog(MainFrame.this, "This test has been completed with " + panel.getCorrectCount() + " questions correct out of " + panel.getTotalCount() + "\nWould you like to save the results?", "Test Completed", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    if(chooser.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
                        file = new File(chooser.getSelectedFile().getName());
                        try(FileInputStream fis = new FileInputStream(file)) {
                            try {
                                ois = new ObjectInputStream(fis);
                            } catch (EOFException e) {

                            }

                            try {
                                if(ois != null) {
                                    entries = (LinkedList<TestEntry>)ois.readObject();
                                }
                            } catch (EOFException e) {

                            }
                            if(entries == null) {
                                entries = new LinkedList<TestEntry>();
                            }
                            entries.add(entry);

                            if(ois != null) {
                                ois.close();
                            }
                        } catch (IOException | ClassNotFoundException e1) {
                            JOptionPane.showMessageDialog(MainFrame.this, "An error occurred! This was the message: " + e1.getClass());
                        }
                        try(FileOutputStream fos = new FileOutputStream(file)) {
                            ObjectOutputStream oos = new ObjectOutputStream(fos);

                            oos.writeObject(entries);

                            oos.close();
                        } catch (IOException e) {
                            JOptionPane.showMessageDialog(MainFrame.this, "An error occurred! This was the message: " + e.getClass());
                        }
                    }
                }

                control.addTestEntry(entry);
                testTable.refresh();
                control.deleteEntries();
            }

            if(panel.getTotalCount() < 158) {
                JOptionPane.showMessageDialog(MainFrame.this, "You did not complete all questions on the test!", "Test Incomplete", JOptionPane.INFORMATION_MESSAGE);
            }

            panel.setCorrectCount(0);
            panel.setIncorrectCount(0);
            panel.setTotalCount(0);

            panel.setCorrectLabel("Correct: " + panel.getCorrectCount());
            panel.setIncorrectLabel("Incorrect: " + panel.getIncorrectCount());

            testing = false;

            roots.clear();
        }

当我在学校网络的本地驱动器上创建一个文件,我可以将测试结果序列化,然后在同一台计算机上运行测试并序列化测试结果,然后就可以了,找到了文件!但是,当我使用另一台计算机运行程序并将结果序列化到我在另一台计算机上创建的本地驱动器文件时,它会给我一个java.io.FileNotFoundException。任何帮助将不胜感激。

0 个答案:

没有答案