How to write detail into file using BufferedWriter and FileWriter in Java?

女生的网名这么多〃 提交于 2019-12-12 02:35:13

问题


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){}
        }
    }

回答1:


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)
    {}
}



回答2:


Try to call

bw.flush();

before closing file. Good practice is to flush stream every time you wrote significant piece of data. In your case add this call to 2 places: into end of the while loop body and after bw.write("Thank You...!").



来源:https://stackoverflow.com/questions/9589227/how-to-write-detail-into-file-using-bufferedwriter-and-filewriter-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!