将医疗账单写入java中的.txt文件

时间:2013-06-11 07:23:27

标签: java swing bufferedwriter

我正在使用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();
                      }
        }
    });

该文件有时会被保存,有时不会有任何信息。任何人都可以帮我解决这个问题。提前谢谢

1 个答案:

答案 0 :(得分: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();
}