簡體   English   中英

寫入文件時Java文件為空

[英]Java file is blank when Writing in a file

感謝幫助。 我正在使用 Java,我想在文件中寫入文本。 我不確定我的代碼有什么問題。 下面的代碼是否足夠,因為它是程序的片段? 我正在嘗試在文件中寫入文本並且沒有錯誤。 當我嘗試打開文件時,文件是空白的。

PharmacyReceipt = 是系統將讀取它並計算總價的文件 PharmacyInvoice = 如果金額超過總價,系統將開具發票並刪除收據文件

謝謝

double change = 0;
                grandTotal = 0;
                try {
                    BufferedReader pharmacyFile = new BufferedReader(new FileReader("pharmacyReceipt.txt"));
                    BufferedWriter write1 = new BufferedWriter(new FileWriter("pharmacyInvoice.txt", true));
                    String input_1;
                    System.out.printf("%-16s %-50.30s %-20s %-20s %-20s\n", "Product Code", "Product Name", "Price", "Quantity", "Net Amount");
                    while ((input_1 = pharmacyFile.readLine()) != null) {

                        String[] data = input_1.split(",");
                        adminObject.setProductCode(data[0]);
                        adminObject.setName(data[1]);
                        adminObject.setPrice(Double.parseDouble(data[2]));
                        adminObject.setQuantity(Double.parseDouble(data[3]));
                        adminObject.setTax(Double.parseDouble(data[4]));
                        adminObject.setDiscount(Double.parseDouble(data[5]));

                        int MAX_CHAR = 40;
                        int maxLength = (adminObject.getName().length() < MAX_CHAR) ? adminObject.getName().length() : MAX_CHAR;
                        //grossAmount = adminObject.getPrice() * (adminObject.getTax()/100);
                        //netAmount = adminObject.getQuantity() * (grossAmount - (grossAmount * adminObject.getDiscount()/100));
                        netAmount = adminObject.getPrice() * adminObject.getQuantity();
                        System.out.printf("%-16s %-50.30s %-20.2f %-20.2f %.2f\n", adminObject.getProductCode(), adminObject.getName().substring(0, maxLength), adminObject.getPrice(), adminObject.getQuantity(), netAmount);
                        //System.out.println(adminObject.getProductCode() + "\t \t " + adminObject.getName() + "\t\t " + adminObject.getPrice() +  "\t\t " + adminObject.getQuantity() +  "\t\t " + adminObject.getTax() + "\t " + adminObject.getDiscount());

                        grandTotal += netAmount;
                    }
                    System.out.printf("\nGrand Total = PHP %.2f\n\n", grandTotal);
                    pharmacyFile.close();

                    System.out.print("Do you want to proceed to print the receipt? ");
                    choice_3 = sc.nextLine();
                    choice_3 = choice_3.toUpperCase();
                    if (choice_3.equals("YES") || choice_3.equals("Y")) {

                        System.out.print("\nHow much is the money? ");
                        double money = sc.nextDouble();

                        if (grandTotal <= money) {

                            BufferedReader groceryFile1 = new BufferedReader(new FileReader("pharmacyReceipt.txt"));
                            System.out.printf("%-16s %-50.30s %-20s %-15s %-20s\n", "Product Code", "Product Name", "Price", "Quantity", "Net Amount");
                            while ((input_1 = groceryFile1.readLine()) != null) {

                                String[] data = input_1.split(",");
                                adminObject.setProductCode(data[0]);
                                adminObject.setName(data[1]);
                                adminObject.setPrice(Double.parseDouble(data[2]));
                                adminObject.setQuantity(Double.parseDouble(data[3]));
                                adminObject.setTax(Double.parseDouble(data[4]));
                                adminObject.setDiscount(Double.parseDouble(data[5]));

                                int MAX_CHAR = 40;
                                int maxLength = (adminObject.getName().length() < MAX_CHAR) ? adminObject.getName().length() : MAX_CHAR;
                                //grossAmount = adminObject.getPrice() * (adminObject.getTax()/100);
                                //netAmount = adminObject.getQuantity() * (grossAmount - (grossAmount * adminObject.getDiscount()/100));
                                netAmount = adminObject.getPrice() * adminObject.getQuantity();
                                write1.write(adminObject.getProductCode() + "," + adminObject.getName() + "," + adminObject.getPrice() + "," + adminObject.getQuantity() + "," + adminObject.getTax() + "," + adminObject.getDiscount() + "," + adminObject.getTax() + "," + adminObject.getDiscount() + "\n");
                                System.out.printf("%-16s %-50.30s %.2f\t\t %.2f\t\t %.2f\n", adminObject.getProductCode(), adminObject.getName().substring(0, maxLength), adminObject.getPrice(), adminObject.getQuantity(), netAmount);
                                //System.out.println(adminObject.getProductCode() + "\t \t " + adminObject.getName() + "\t\t " + adminObject.getPrice() +  "\t\t " + adminObject.getQuantity() +  "\t\t " + adminObject.getTax() + "\t " + adminObject.getDiscount());
                                
                            }
                            write1.write("___________________________________________________");
                            write1.write("\nGrand Total = PHP %.2f\n\n" + grandTotal);
                            write1.write("Money: " + money + "\n");
                            write1.write("Change: " + (change = money - grandTotal) + "\n");
                            write1.write("___________________________________________________\n");
                            System.out.println("___________________________________________________\n");
                            System.out.printf("\nGrand Total = PHP %.2f\n\n", grandTotal);
                            System.out.println("Money: " + money + "\n");
                            System.out.println("Change: " + (change = money - grandTotal) + "\n");
                            System.out.println("___________________________________________________\n");
                            
                            
                        }
                    } else {
                        System.out.println("___________________________________________________\n");
                        System.out.println("***Money exceeds the amount.***");
                        System.out.println("___________________________________________________\n");
                    }
                    pharmacyFile.close();

                } catch (FileNotFoundException e) {
                    System.out.println("File Not Found" + e);
                } catch (IOException e) {
                    System.out.println("File Not Found");
                } catch (NumberFormatException e) {
                    System.out.println(e);
                } catch (Exception e) {
                    System.out.println(e);
                }
                break;

您必須在最后通過writer1.close()關閉writer1.close() 因為您有一個 BufferedWriter,它不會總是立即寫入文件。 例如,如果您的程序在它可以寫入之前退出,您的內容到文件中,它將保持空白。

此外,您通常應該始終在最后關閉此類資源,因為它們可能會導致內存泄漏和其他問題。 你可以通過:

try (FileWriter writer1 = new FileWriter(new File("blablub")) {
    // ... do your stuff with the writer
}

這稱為 try-with-resources 子句,最終會自動關閉資源。 更明確的版本是:

FileWriter writer1 = new FileWriter(newFile("blablub"));
try {
    // ... do your stuff with the writer
} catch (Exception ex) {
   ...
} finally {
   writer1.close();
}

編輯:可能是,您認為您正在關閉我現在看到的 writer1,但實際上您有兩次對 pharmacyFile.close() 的調用。 也許這是你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM