无法打印到文本文件

时间:2022-08-07 10:47:11

Input file ViewOrder.dat contains

输入文件ViewOrder.dat包含

inv1102;p1600;brush;2;26.0;Partially Full

This is the java Code

这是java代码

public void viewBackOrder() {

    File fileViewOrder = new File("ViewOrder.dat");
    File fileViewBackOrder = new File("ViewBackOrder.dat");
    Scanner input = new Scanner(System.in);

    String orderNo, itemNo, itemName, itemQty, itemPrice, status;

    try {

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileViewOrder)));
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileViewBackOrder));
        String line = null;

        while ((line = br.readLine()) != null) {

            String tokens[] = line.split(";");

            orderNo = tokens[0];
            itemNo = tokens[1];
            itemName = tokens[2];
            itemQty = tokens[3];
            itemPrice = tokens[4];
            status = tokens[5];

            System.out.println("Order No: [" + orderNo + "]" + " Item No: [" + itemNo + "]" + " Back Order No: [" + itemQty + "]\n");

        }

        System.out.print("Print Back Order List to file: ");
        String choice = input.next();
        if (choice.equalsIgnoreCase("y")) {

            while ((line = br.readLine()) != null) {

                String tokens[] = line.split(";");

                orderNo = tokens[0];
                itemNo = tokens[1];
                itemName = tokens[2];
                itemQty = tokens[3];
                itemPrice = tokens[4];
                status = tokens[5];


                bw.write("Order No: [" + orderNo + "]" + " Item No: [" + itemNo + "]" + " Back Order No: [" + itemQty + "]");
                bw.newLine();
                bw.flush();
                bw.close();
            }

            System.out.println("Successful add to file!");

        } else if (choice.equalsIgnoreCase("n")) {

            System.out.println("Error");
        }

    } catch (Exception e) {

        System.out.println("Error");
    }

}

Is there any error because it seems like I couldn't find out the problem or did i override it everytime i run? Or should i change the variable name?

是否有任何错误,因为我似乎找不到问题或者我每次运行时都覆盖它?或者我应该更改变量名称?

1 个解决方案

#1


2  

You're consuming the BufferedReader br input twice.

您正在使用两次BufferedReader br输入。

First time you successfully print the entire input file to the screen.

第一次成功将整个输入文件打印到屏幕上。

But second time (when you try to write) your code fail to read it again, because you have already reached the end of file.

但第二次(当你尝试写)时,你的代码无法再次读取它,因为你已经到了文件的末尾。

You should close and reopen it, I strongly suggest to have a look at Java try-with-resource statement.

您应该关闭并重新打开它,我强烈建议您查看Java try-with-resource语句。

Aside of this problem, I also suggest to create the BufferedWriter bw only when the user answer "yes" to the question (because if your user answer "no" you just create a useless empty file).

除了这个问题,我还建议只有当用户对问题回答“是”时才创建BufferedWriter bw(因为如果你的用户回答“否”你只是创建一个无用的空文件)。

#1


2  

You're consuming the BufferedReader br input twice.

您正在使用两次BufferedReader br输入。

First time you successfully print the entire input file to the screen.

第一次成功将整个输入文件打印到屏幕上。

But second time (when you try to write) your code fail to read it again, because you have already reached the end of file.

但第二次(当你尝试写)时,你的代码无法再次读取它,因为你已经到了文件的末尾。

You should close and reopen it, I strongly suggest to have a look at Java try-with-resource statement.

您应该关闭并重新打开它,我强烈建议您查看Java try-with-resource语句。

Aside of this problem, I also suggest to create the BufferedWriter bw only when the user answer "yes" to the question (because if your user answer "no" you just create a useless empty file).

除了这个问题,我还建议只有当用户对问题回答“是”时才创建BufferedWriter bw(因为如果你的用户回答“否”你只是创建一个无用的空文件)。