0

これらの詳細をファイルに書き込むのに問題があります。この詳細をファイルに書き込もうとしていますが、この関数が特定の場所にファイルを作成する方法がありますが、ファイルには何も書き込まれません。

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

2 に答える 2

0

電話してみてください

bw.flush();

ファイルを閉じる前に。重要なデータを書き込むたびにストリームをフラッシュすることをお勧めします。あなたの場合、この呼び出しを2つの場所に追加します:whileループ本体の終わりと後bw.write("Thank You...!")

于 2012-03-06T18:05:41.673 に答える
0

コードの間違いは、BufferedWriterのインスタンスを閉じる前にFileWriterのインスタンスを閉じたことです。bw.close()とfw.close()の位置を単純に入れ替えれば機能します。finallyブロックは次のようになります。

finally
{
    try
    {
       bw.close();
       fw.close();
    }
    catch(Exception e)
    {}
}
于 2012-03-06T18:11:50.273 に答える