简体   繁体   English

无法打印到文本文件

[英]Fail to print to text file

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? 还是应该更改变量名称?

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 (因为如果用户回答“否”,那么您只会创建一个无用的空文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM