【问题标题】:How to write detail into file using BufferedWriter and FileWriter in Java?如何在 Java 中使用 BufferedWriter 和 FileWriter 将详细信息写入文件?
【发布时间】:2012-03-24 06:11:06
【问题描述】:

我在文件中写入这些详细信息时遇到问题。 我试图在文件中写入这个细节,但是这个函数如何在特定位置创建文件,但它没有向文件中写入任何内容。

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

【问题讨论】:

    标签: java file-io filewriter bufferedwriter


    【解决方案1】:

    尝试调用

    bw.flush();

    在关闭文件之前。好的做法是在每次写入重要数据时刷新流。在您的情况下,将此调用添加到 2 个位置:while 循环体的末尾和 bw.write("Thank You...!") 之后。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 你能帮我写代码来打印同一个文件吗?我已经完成了 Oracle 的 2DPrinting 教程,但我想打印我生成的相同 txt 文件。所以你能帮我解决同样的问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2021-06-01
      • 2023-01-09
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      相关资源
      最近更新 更多