使用保存按钮将arraylist保存到C:驱动器上的.dat文件

时间:2013-12-06 05:17:10

标签: java netbeans save overwrite

我很难将我的文件保存到我的C:驱动器使用我的保存按钮。我的动作监听器看起来像这样

saveButton.addActionListener(
    new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            File savedFile = new File("C:\\data\\inventory.dat");
            if (savedFile.exists() == false)
            {
                try
                {
                    savedFile.createNewFile();
                    outputText.append("The file has been saved\n");
                }
                catch (IOException ex)
                {
                    Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            else
            {
                if (savedFile.exists() == true)
                {
                    try
                    {
                        savedFile.createNewFile();
                        outputText.append("The file already exists\n and has been overwritten\n");
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
);

我一直遇到的问题是

Dec 06, 2013 12:14:16 AM javaguifixed.JavaGUIFixed$8 actionPerformed
SEVERE: null
java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1006)

有人能指出我正确的方向吗?为什么这不节约?本质上它应该是创建目录以及文件,如果它不存在,它应该覆盖它,如果它存在。

1 个答案:

答案 0 :(得分:0)

我做了一些游戏,并让它工作。以下是我的代码

 saveButton.addActionListener(
            new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    File savedFile = new File("C:\\Users\\kerinr\\Documents\\data\\inventory.dat");
                    if (savedFile.exists() == false) {
                        try {
                            savedFile.createNewFile();

                            outputText.append("The file has been saved\n");
                        } catch (IOException ex) {
                            Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    } else {
                        if (savedFile.exists() == true) {
                            try {
                                savedFile.createNewFile();
                                outputText.append("The file already exists\n and has been overwritten\n");
                            } catch (IOException ex) {
                                Logger.getLogger(JavaGUIFixed.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                    Writer writer = null;

                    try {
                        writer = new BufferedWriter(new OutputStreamWriter(
                                        new FileOutputStream("C:\\Users\\kerinr\\Documents\\data\\inventory.dat"), "utf-8"));
                        for (int i = 0; i < inventory.size(); i++) {
                            writer.write(inventory.get(i).itemName + " \r\n");
                            writer.write(inventory.get(i).inStock + " \r\n");
                            writer.write(inventory.get(i).itemNumber + " \r\n");
                            writer.write(inventory.get(i).unitPrice + " \r\n");
                            writer.write(inventory.get(i).restockingFee + " \r\n");
                            writer.write(inventory.get(i).inventoryValue + " \r\n");

                        }

                    } catch (IOException ex) {
                        // report
                    } finally {
                        try {
                            writer.close();
                        } catch (Exception ex) {
                        }
                    }
                }

            });

我注意到的一件事是文件夹&#34;数据&#34;必须已经到位。如果不是那么它将不保存文件。