gpt4 book ai didi

java - 用java将医疗账单写入.txt文件

转载 作者:行者123 更新时间:2023-12-01 14:27:55 25 4
gpt4 key购买 nike

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

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

最佳答案

在每次迭代之后和结束时添加 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();
}

关于java - 用java将医疗账单写入.txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com