0

我正在用java创建一个医疗计费软件,我需要将创建的账单保存到一个文本文件中以备将来使用。文件在波动。每次执行程序时都不会保存。如果有人告诉我保存文件的更好平台是什么,我也想知道。

我将账单保存到文本文件的代码如下

    final JButton cmdSave = new JButton("Save Bill to File");
    cmdSave.setBounds(240,300,180,25);
    cmdSave.setFont(f14);
    cmdSave.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            try 
              {
                        File file = new File("c:/Bill.txt");

                        // if file doesnt exists, then create it
                        if (!file.exists()) 
                        {
                                 file.createNewFile();
                        }

                        FileWriter fw = new FileWriter(file.getAbsoluteFile());
                        BufferedWriter bw = new BufferedWriter(fw);
                        Date today = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("dd MMMMM yyyy");
                        SimpleDateFormat formatter1 = new SimpleDateFormat("h:mm a");
                        bw.write("G.D.Enterprise");
                        bw.newLine();
                        bw.write("Date: " + formatter.format(today));
                        bw.newLine();
                        bw.write("Time: " + formatter1.format(today));
                        bw.newLine();
                        bw.newLine();
                        bw.write("Residence number: 033 2522 - 4738");
                        bw.newLine();
                        bw.write("Office number: 033 2521-5254");
                        bw.newLine();
                        bw.write("Office Address: 175/A Laketown, Block - A, Kolkata- 700054");
                        bw.newLine();

                        ComboBoxModel model = cmboSerial.getModel();//Serial number combobox
                        ComboBoxModel model1 = cmbo1.getModel();//Batch no combobox
                        ComboBoxModel model2 = cmbo2.getModel();//Quantity combobox
                        ComboBoxModel model3 = cmbo3.getModel();//Description combobox
                        ComboBoxModel model4 = cmbo4.getModel();//Rate combobox
                        ComboBoxModel model5 = cmbo5.getModel();//VAT?TAX combobox
                        ComboBoxModel model6 = cmbo6.getModel();//Dicount combobox
                        ComboBoxModel model7 = cmbo7.getModel();//Amount combobox

                        int size = model.getSize();
                       for(int i=0;i<size;i++) {
                        Object element = (String) model.getElementAt(i);//Serial number combobox
                        Object element1 = (String) model1.getElementAt(i);//Batch no combobox
                        Object element2 = (String) model2.getElementAt(i);//Quantity combobox
                        Object element3 = (String) model3.getElementAt(i);//Description combobox
                        Object element4 = (String) model4.getElementAt(i);//Rate combobox
                        Object element5 = (String) model5.getElementAt(i);//VAT?TAX combobox
                        Object element6 = (String) model6.getElementAt(i);//Discount combobox
                        Object element7 = (String) model7.getElementAt(i);//Amount combobox

                        bw.newLine();
                        bw.write((String) element + ") ");
                        bw.write((String) element3);
                        bw.newLine();
                        bw.write("Batch number = " + (String) element1);
                        bw.newLine();
                        bw.write("Rate = Rs." + (String) element4);
                        bw.newLine();
                        bw.write("Quantity = " + (String) element2);
                        bw.newLine();
                        bw.write("Dicount = Rs." + (String) element7);
                        bw.newLine();
                        bw.write("VAT/TAX = Rs." + (String) element5);
                        bw.newLine();
                        bw.write("Vat amount = Rs." + (Integer.parseInt((String) element5)) * (Integer.parseInt((String) element2)));
                        bw.newLine();
                        bw.newLine();
                        bw.write("Amount = " + (String) element7);
                        bw.newLine();
                        bw.newLine();
                    }
                    bw.write("Amount to be paid = Rs." + cmbo8.getText());
                    bw.newLine();
                    bw.close();
                      } 
                      catch (IOException ex) 
                      {
                        ex.printStackTrace();
                      }
        }
    });

该文件有时会被保存,有时不包含或不包含任何信息。谁能帮我解决这个问题。先感谢您

4

1 回答 1

1

在每次迭代后添加bw.flush()调用并在完成时添加并查看。

for(int i=0;i<size;i++) {
    Object element = (String) model.getElementAt(i);//Serial number combobox
    Object element1 = (String) model1.getElementAt(i);//Batch no combobox
    Object element2 = (String) model2.getElementAt(i);//Quantity combobox
    Object element3 = (String) model3.getElementAt(i);//Description combobox
    Object element4 = (String) model4.getElementAt(i);//Rate combobox
    Object element5 = (String) model5.getElementAt(i);//VAT?TAX combobox
    Object element6 = (String) model6.getElementAt(i);//Discount combobox
    Object element7 = (String) model7.getElementAt(i);//Amount combobox

    bw.newLine();
    bw.write((String) element + ") ");
    bw.write((String) element3);
    bw.newLine();
    bw.write("Batch number = " + (String) element1);
    bw.newLine();
    bw.write("Rate = Rs." + (String) element4);
    bw.newLine();
    bw.write("Quantity = " + (String) element2);
    bw.newLine();
    bw.write("Dicount = Rs." + (String) element7);
    bw.newLine();
    bw.write("VAT/TAX = Rs." + (String) element5);
    bw.newLine();
    bw.write("Vat amount = Rs." + (Integer.parseInt((String) element5)) * (Integer.parseInt((String) element2)));
    bw.newLine();
    bw.newLine();
    bw.write("Amount = " + (String) element7);
    bw.newLine();
    bw.newLine();
    bw.flush() // Flush after Each Iteration
}
bw.write("Amount to be paid = Rs." + cmbo8.getText());
bw.newLine();
bw.flush() // Flush at the end

这也不是必需的,因为FileWriter如果目标文件不存在,它将创建它。

// if file doesnt exists, then create it
if (!file.exists()) 
{
    file.createNewFile();
}
于 2013-06-11T07:31:18.123 回答