Jump to content

Need help with outputting the contents of an array to file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
hollywood

hollywood

    Newbie

  • Members
  • Pip
  • 1 posts
I am having problems writing the contents of an array to file.

The following is the code I am using to show the array on the screen with correct formatting:

public void output()

    {

    System.out.println("Item Cost  Quantity  Description" );

    System.out.println("==================================================" );

    for (int outputIndex = 0; outputIndex < countItems; outputIndex++)

        {

        System.out.printf("%9.2f %10d   %s\n", items[outputIndex].getCost(), items[outputIndex].getQuantity(), items[outputIndex].getDescription());

        }

    System.out.println("==================================================" );

    System.out.println(" ");

    }


Which works just fine. However, when I use similar code for outputting to file it fails to write anything and instead saves a blank page in the file:


public void save() throws IOException

    {

    FileWriter fwriter = new FileWriter("shopping.dat");

    PrintWriter outputFile = new PrintWriter(fwriter);

 

    for (int saveIndex = 0; saveIndex < countItems; saveIndex++)

        {

        outputFile.printf("%9.2f %10d   %s\n", items[saveIndex].getCost(), items[saveIndex].getQuantity(), items[saveIndex].getDescription());

        }

    upToDate = true;

    }


Please help me???

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Try adding
outputFile.close(); 
in the end