Computer Science Canada

output.println doesn't seem to save data to file

Author:  username123 [ Thu Nov 12, 2015 9:15 am ]
Post subject:  output.println doesn't seem to save data to file

What the title says. When I try to display the contents of itemList.txt, nothing shows up.
Java:

PrintWriter output = new PrintWriter(itemList);
    double price = 0;
    String itemNumber;
   
    System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
    answer = input.next();
   
    while(!answer.equals("N")||!answer.equals("n"))
    {   
      if(answer.equals("Monitors")||answer.equals("monitors"))
      {
        //Displays the list of monitors
        for(int i = 0; i < 3; i++)
        {
          System.out.println(lines[i]);
        }
       
        //Buying monitors
        System.out.println("Enter the item number of an item to see its specifications and add it to your shopping cart. Press N to stop browsing.");
        itemNumber = input.next();
       
        while(!itemNumber.equals("N")||!itemNumber.equals("n"))
        {
          if(itemNumber.equals("N82E16824009659"))
          {
            System.out.println("Acer B6 Series B286HK ymjdpprz ($499.99)\n- 28\"\n- Widescreen\n- 4k resolution(3840x2160)\n- LED backlight\n- DVI, HDMI, DP, miniDP, SPK, MHL\n- USB 3.0\n- LCD\n- 2ms response");
            System.out.println("Would you like to purchase this item?");
            answer = input.next();
            if(answer.equals("yes")||answer.equals("Yes"))
            {
              System.out.println("How many of this item do you want?");
              int numItem = input.nextInt();
              price = price + 499.99 * numItem;
              output.println(numItem + "x Acer B6 Series B286HK ymjdpprz, $" + 499.99 * numItem);
            }
            System.out.println("Enter the item number of an item to see its specifications and add it to your shopping cart. Press N to stop browsing.");
            itemNumber = input.next();
          }

Author:  Zren [ Thu Nov 12, 2015 9:28 am ]
Post subject:  RE:output.println doesn\'t seem to save data to file

write()/println() don't actually write to the file, they just keep it in memory until you've flushed the stream which will write all that's in memory to the file. You also need to close the file once you're done i/o operations on it.

Closing the stream automatically flushes the stream.

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#close()


: