从GUI读取到文本文件错误

时间:2015-12-12 13:29:17

标签: java user-interface printwriter

我的'保存'按钮的actionPerformed有问题。当我编译这段代码时没有错误但是当我点击“保存”按钮时,根本没有创建文件。我从文本字段文本文件???

中保存输入数据的正确方法是什么
saveButton.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e) {

                 //assign value from JTextField to new variable to link all the data members to GUI
                 String strtype = typeCombo.getName();
                 String strcode = jtfCode.getText();
                 String strbrand = jtfBrand.getText();
                 String strEngNum = jtfEngineNumber.getText();
                 String strEngType = jtfEngineType.getText();
                 String strmaxHorsepower = jtfMaxHorsepower.getText();
                 String strrangePerFillUp = jtfRangePerFillUp.getText();
                 String strprice = jtfPrice.getText();

                 //double maxHorsepowerM = Double.parseDouble(jtfMaxHorsepower.getText());
                 //double rangePerFillUpM = Double.parseDouble(jtfRangePerFillUp.getText());
                 //double priceM = Double.parseDouble(jtfPrice.getText());



                 if(strcode == null || strcode.equals("")
                                    || strbrand == null || strbrand.equals("") || strEngNum == null || strEngNum.equals("")
                                    || strEngType == null || strEngType.equals("") || (strmaxHorsepower == null || strmaxHorsepower.equals(""))
                                    && (strrangePerFillUp == null || strrangePerFillUp.equals("")) || strprice == null || strprice.equals("")) {


                     //exception for empty field
                    JOptionPane.showMessageDialog(null,"Inserting data is incomplete. Please fully insert the data",
                    "Wrong input", JOptionPane.WARNING_MESSAGE);

                }
                else {

                    double dbmaxHorsepower = Double.parseDouble(strmaxHorsepower);
                    double dbrangePerFillUp = Double.parseDouble(strrangePerFillUp);
                    double dbprice = Double.parseDouble(strprice);

                     //Invoke the composition into GUI with data members
                     Engine eng = new Engine(strEngNum,strEngType);
                     //Invoke the superclass into GUI with their data members
                     Car car = new Car(eng,dbprice,strcode,strbrand);
                     //Invoke the subclasses into GUI together with their data members
                     SportsCar sportsCar = new SportsCar(dbmaxHorsepower,eng,dbprice);
                     HybridCar hybridCar = new HybridCar(dbrangePerFillUp,eng,dbprice); 

                     //PrintWriter fw = null;



                    //declaring object to write data to file
                    //CustomFileWriter fw = new CustomFileWriter("D:\\UniKL_Bachelor\\OOP_Project\\Project\\GroupProject\\BB_Automobil's_Inventory Record.txt");
                    File file = new File("D:\\UniKL_Bachelor\\OOP_Project\\Project\\GroupProject\\BB_Automobil's_Inventory Record.txt");
                    if (!file.exists())
                        file.createNewFile();

                    CustomFileWriter fw = new CustomFileWriter(file);
                    BufferedWriter bw = new BufferedWriter(fw);



                    //either fill maxHorsepower(SportsCar) or rangePerFillUp(HybridCar)
                    //if(strmaxHorsepower != null) 
                    if (typeCombo.getSelectedItem().toString()
                            .equalsIgnoreCase("Sports Car"))
                    {
                        //String input = typeN +", "+ codeM +", "+ brandM +", "+ engineNumberN +", "
                                    //+ engineTypeN +", "+ maxHorsepowerN +", "+ priceN;
                         bw.writeToFile(FILE_NAME, sportsCar, true);
                    }
                    //else if(strrangePerFillUp != null)
                    if (typeCombo.getSelectedItem().toString()
                            .equalsIgnoreCase("Hybrid Car"))
                    {
                          //String input = typeN +", "+ codeM +", "+ brandM +", "+ engineNumberN +", "
                                    //+ engineTypeN +", "+ rangePerFillUpN +", "+ priceN;
                          bw.writeToFile(FILE_NAME, hybridCar, true);
                    }

                } 


                }

            });   

2 个答案:

答案 0 :(得分:0)

File("D:\\...\\GroupProject\\BB_Automobil's_Inventory Record.txt");

应该成为

File("D:\\...\\GroupProject\\BB_Automobil\'s_Inventory Record.txt");

因为'是转义字符。

答案 1 :(得分:0)

您实际遇到的真正错误不是编译时错误。但这是一个运行时错误。在声明File对象之前,必须声明BufferedFileReader对象或BufferedFileWriter对象。只有这样你才能读取或写入文件。