How to make it so that FileWriter doesn't continuously replace line one with input instead of just moving onto the next line?

29 views Asked by At

I'm using FileWriter and I have it so that when a user answers a question, the linked text is added to the attached text file but instead of each .write(""); going to the next line in the txt file, it just keeps replacing line 1, when I use line separator, or (\n) it just adds blank lines underneath while continuing the replace the text on line 1.

System.out.println("Please choose your sub size \n 1 - six inch ($7.19) \n 2 - twelve inch ($10.99) \n  Please enter '1' or '2'");
sub.size = input.nextLine();

// filtering all input other than '1' and '2'
while (!sub.size.equals("1") && !sub.size.equals("2")) {
    System.out.println("Please enter '1' or '2'");
    sub.size = input.nextLine();
}

//adding to the price depending on chosen size
if (sub.size.equals("1")) {
    sub.price = sub.price + 7.19;
    try {
        FileWriter orderTaker = new FileWriter("subOrder.txt");
        orderTaker.write("6 inch Sub");
        //orderTaker.write(System.lineSeparator());
        orderTaker.close();
    } catch(IOException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
    }
}

First input After second input

Tried bufferedWriter off of someone's suggestion, have tried (\n), have tried (\r\n), have tried lineSeparator

1

There are 1 answers

0
Cardinal System On

The solution to your problem lies in the constructor you are using. If you do not want to override the existing file contents, you need to use the FileWriter(File, boolean) constructor, which takes a boolean parameter called append. Set append to true, and it will add your data to the end of the file instead of overwriting it.

A better solution, however, would be to keep your FileWriter opened until you are finish collecting all the orders. Opening and closing a file handle repeatedly will result in bad performance.

Here's an example:

Scanner input = new Scanner(System.in);
boolean acceptingOrders = true;

try (FileWriter orderTaker = new FileWriter("subOrder.txt", true)) {
    while (acceptingOrders) {
        System.out.println("1 - six inch ($7.19)");
        System.out.println("2 - twelve inch ($10.99)");
        System.out.println("Please enter '1' or '2' or 'done'");

        boolean invalidInput = true;
        String userInput = input.nextLine();
        do {
            if (userInput.equals("1")) {
                sub.size = userInput;
                sub.price = sub.price + 7.19;
                orderTaker.write("6 inch Sub\n");

                invalidInput = false;
            } else if (userInput.equals("2")) {
                sub.size = userInput;
                sub.price = sub.price + 10.99;
                orderTaker.write("12 inch Sub\n");

                invalidInput = false;
            } else if (userInput.equalsIgnoreCase("done")) {
                acceptingOrders = false;
                invalidInput = false;
            } else {
                System.out.println("Please enter '1' or '2' or 'done'");
                userInput = input.nextLine();
            }
        } while (invalidInput);
    }
} catch (IOException e) {
    System.out.println("An error occurred.");
    e.printStackTrace();
}