如何在Java中使用BufferedWriter和FileWriter将详细信息写入文件? [英] How to write detail into file using BufferedWriter and FileWriter in Java?

查看:236
本文介绍了如何在Java中使用BufferedWriter和FileWriter将详细信息写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将这些详细信息写入File时遇到问题. 我正在尝试在文件中写入此详细信息,但有些功能会如何在特定位置创建文件,但不会在文件中写入任何内容.

I have a problem to write these detail in File. I am trying to write this detail in file but some how this function create File in specific location but its not writing anything into the file.

public void writeBillToFile(double amount , double billingAmount,double taxAmount,
                                   double discount ,double transactionID , double billingNumber , 
                                   int customerID , String tableNumber ,ArrayList listObject  )
    {
        FileWriter fw=null ;
        BufferedWriter bw =null;
        Date d=new Date();        
        long currentTimestamp=d.getTime();
        try{

            fw = new FileWriter("D:/study/ADVANCE_JAVA/PrOgRaMs/WEB_APPS/00_COS-THE MEGA PROJECT/COS_March_03/GeneratedBill/bill"+currentTimestamp+".txt" , true);        
            bw= new BufferedWriter(fw);

            System.out.println("Date and Time :: "+d.toString() +"\t Bill No :: "+billingNumber+"\t Transaction ID :: "+transactionID+"\n");
            bw.write("Date and Time :: "+d.toString() +" Bill No::"+billingNumber+" Transaction ID::"+transactionID);
            bw.newLine();
            Iterator  iteratorObject= listObject.iterator();
            while(iteratorObject.hasNext())        
            {          
                ItemInSessionModel itemObject = (ItemInSessionModel)iteratorObject.next();
                bw.write(itemObject.getItemName()+"    "+itemObject.getItemQty()+"     "+itemObject.getItemRate()+"     "+(itemObject.getItemRate()*itemObject.getItemQty()));
                bw.newLine();
            }

            bw.write("Total Amount ::"+amount);
            bw.newLine();
            bw.write("Discount     ::"+discount);
            bw.newLine();
            bw.write("TAX          ::"+taxAmount);
            bw.newLine();
            bw.write("Bill Amount  ::"+billingAmount);
            bw.newLine();
            bw.write("Thank You...!");
            System.out.println("Successfully Writen in File...!");
        }catch(Exception e)
        {
            System.out.println("Exception in FILE IO :: "+e);
        }
        finally
        {
            try{
            fw.close();
            bw.close();
            }catch(Exception e){}
        }
    }

推荐答案

代码中的错误是您在关闭BufferedWriter实例之前已关闭FileWriter实例.如果您只是交换bw.close()和fw.close()的位置,它将起作用. 您的finally块应如下所示:

The mistake in your code is that you have closed the instance of FileWriter before closing the instance of BufferedWriter. It will work if you simply swap position of bw.close() and fw.close(); Your finally block should look like as follows :

finally
{
    try
    {
       bw.close();
       fw.close();
    }
    catch(Exception e)
    {}
}

这篇关于如何在Java中使用BufferedWriter和FileWriter将详细信息写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆