第二个缓冲的作家将无法工作

second buffered writer won't work

我的简单 I/O 库存程序有点问题。该程序将用户输入的输出写入充当 "inventory" 的文本文件。我能够做到。但是,当我尝试在同一文件夹中创建和追加不同的文本文件时,它不会将输入写入新的文本文件。我知道我真的缺乏经验来正确解释我的担忧。但我正在尽力而为,所以基本上,我只想将我输入的信息打印为 "sales" 到同一路径文件夹中的第二个 text.file 中。如果您检查 运行 我的代码,希望您能得到一些想法。该代码正在运行,但正如我所说,唯一的问题是 input/output 在不同的文本文件上。我将衷心感谢您的帮助。谢谢

卡尔

这是我的代码:

   import java.io.BufferedOutputStream;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.*;

    import static java.nio.file.StandardOpenOption.*;

    import java.text.*;
    import java.util.*;

    public class Inventorypos {

        public static void main(String[]args) {

            Scanner input = new Scanner(System.in);
            String File ="D:\Copro2\Inventory.txt";
            String File2 ="D:\Copro2\Sales.txt";

            Path file = Paths.get("D:\Copro2\Inventory.txt");
            Path salefile = Paths.get("D:\Copro2\Sales.txt");


            String st = " ";
            String sH = " ";
            String sF = " ";
            String sS = " ";
            String delimeter = "................ ";
            int itemnum;
            int itemname;
            String H="Hamburger";
            String F="French Fries";
            String S="Softdrinks";
            int menu;
            long menu2 = 0;
            final long QUIT = 5;{


        {


        try{

            Runtime rt=Runtime.getRuntime();

            OutputStream invent = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
                    BufferedWriter writer = new
                BufferedWriter(new OutputStreamWriter(invent));

//so here is my problem here, I couldn't make this work

            OutputStream sale = new BufferedOutputStream(Files.newOutputStream(salefile, CREATE,StandardOpenOption.APPEND));
                    BufferedWriter writer2 = new
                BufferedWriter(new OutputStreamWriter(sale));


                    String sa= "---- Sales ----";
                    writer2.write(sa);
                    writer2.newLine();




                    String in= "---- Inventory ----";
                    writer.write(in);
                    writer.newLine();



                    System.out.println("---- Page Menu ----");
                    System.out.println("1. Add Inventory \n2. Sell \n3. View Inventory \n4. View Sales \n5. Exit ");
                    System.out.print("Enter selection: ");          
                    menu= input.nextInt();

                    while(menu2 != QUIT){



                    if(menu==1) {
                        System.out.println("Add Inventory: " );
                        System.out.print("1.) Hamburger \n2.)French Fries \n3.)Softdrinks\n");
                        System.out.println("Enter Item: " );
                        itemname= input.nextInt();


                        if (itemname == 1){
                            System.out.print("How many do you want to store? ");
                            itemnum = input.nextInt();  

                        sH = H + delimeter + itemnum;

                        writer.write(sH, 0, sH.length());
                        writer.newLine();
                        }
                        else if (itemname == 2){
                            System.out.print("How many do you want to store? ");
                            itemnum = input.nextInt();  
                            sF = F + delimeter + itemnum;

                            writer.write(sF, 0, sF.length());
                            writer.newLine();
                        }
                        else if (itemname == 3){
                            System.out.print("How many do you want to store? ");
                            itemnum = input.nextInt();  
                            sS = S + delimeter + itemnum;

                            writer.write(sS, 0, sS.length());
                            writer.newLine();

                        }
                        else{
                            System.out.println("Invalid input!");
                            menu=1;
                        }
                        System.out.print("Enter 1 to store another item, 2 to view inventory, 3 sell,4 to quit: ");
                        menu2 = input.nextLong();       
                        if (menu2==1){
                             menu=1;

                        }
                        else if (menu2==2){
                            menu=3;
                        }
                        else if (menu2==3){
                             menu=6;
                        }
                        else if (menu2==4){
                             menu2=QUIT;
                        }
                        else{
                            System.out.println("Invalid Input!");
                        }


                    }
                if(menu==2){



                        System.out.print("---- Food Menu ----");
                        System.out.println("\n1.) Hamburger P25 \n2.) French Fries P25 \n3.) Softdrinks P25");
                        System.out.println("Enter your order: ");
                        int ord = input.nextInt();



                        if (ord==1){

                            System.out.println("How many?");
                            int hm = input.nextInt();
                            int totph = hm*25;

                            /*String intwo = "---- Updated inventory ----"; 

                            writer.write(intwo);
                            writer.newLine();

                            /*int left = 
                            String nI = H+delimeter+*/


                            st = H+hm+ delimeter+ totph;

                            writer2.write(st,0,st.length());
                            writer2.newLine();
                            System.out.println("You purchased "+hm+" "+H+"s with the total price of "+totph+".");

                        }
                        else if (ord==2){

                            System.out.println("How many?");
                            int hm = input.nextInt();
                            int totph = hm*25;

                            /*String intwo = "---- Updated inventory ----"; 

                            writer.write(intwo);
                            writer.newLine();

                            /*int left = 
                            String nI = H+delimeter+*/


                            st = F + hm + delimeter + totph;

                            writer2.write(st, 0, st.length());
                            writer2.newLine();
                            System.out.println("You purchased "+hm+" "+F+" with the total price of "+totph+".");

                        }
                        else if (ord==3){

                                System.out.println("How many?");
                                int hm = input.nextInt();
                                int totph = hm*25;

                                /*String intwo = "---- Updated inventory ----"; 

                                writer.write(intwo);
                                writer.newLine();

                                /*int left = 
                                String nI = H+delimeter+*/


                                st = S+hm+ delimeter+ totph;

                                writer2.write(st,0,st.length());
                                writer2.newLine();
                                System.out.println("You purchased "+hm+" "+S+"s with the total price of "+totph+".");

                            }
                        else{
                            System.out.println("Invalid input!");
                            menu=2;
                        }

                            System.out.print("Enter 1 to go back to the food menu, 2 to view sales, 3 to go back to the main option or 4 to quit: ");
                            menu2 = input.nextInt();        
                            if (menu2==1){
                                 menu=2;
                            }
                            else if (menu2==2){
                                 menu=4;
                            }else if (menu2==4){
                                 menu2=QUIT;
                            }
                            else if (menu2==3){
                                 menu=6;
                            }
                            else{
                                System.out.println("Invalid Input!");
                            }
                }
            if(menu==3){
                {   
                    menu2 = QUIT;
                    writer.close(); 
                    Process p=rt.exec("notepad "+File);
                    System.out.println("Do you want to continue? y for yes, n for no");
                    String con = input.next();

                    if (con.equalsIgnoreCase("y")){
                        main(null);
                    }
                    else if(con.equalsIgnoreCase("n")){
                        menu2=QUIT;
                    }

                    }
            }
                if (menu==4){
                {   
                    menu2 = QUIT;
                    writer.close();
                    Process p=rt.exec("notepad "+File2);
                    System.out.println("Do you want to continue? y for yes, n for no");
                    String con = input.next();

                    if (con.equalsIgnoreCase("y")){
                        main(null);
                    }
                    else if(con.equalsIgnoreCase("n")){
                        menu2=QUIT;
                    }

                    }
                }
                if (menu==5){
                    System.exit(0);
                }
                else if (menu ==6){
                        main(null);

                }
                else{
                    System.out.println("Invalid Input!");
                }
                }
            }catch (Exception e){
                        System.out.println("Message: " +e);
        }
    }

    }
    }
    }

这就是 BufferedWriter 对象的特殊之处。为了提高效率,他们将信息存储在 "buffers" 或临时信息堆中,然后当缓冲区超过一定大小时,它 "flushes" 数据或将其写入文件。调用 flush() 方法或 close() 方法时完成刷新数据。在你的代码中,我不相信你对你的作者调用了 close() 而不管类型如何,你实际上都会这样做。

您可以这样调用它:

writer.close();

或者如果你只是想在不关闭写入器的情况下保存数据,你可以调用:

writer.flush();