Hey Stack Overflow people, I'm having a bit of trouble understanding how writing and appending files works. Here is what I've been asked to do
• Save (append) the data for the contract to the text-based summary file (contracts.txt).
Each line in the file must hold the details for a single contract with the following information separated by spaces or tabs:
• Contract Date (today’s date - see technical details for format).
• Package (1=Small, 2=Medium and 3=Large)
• Data Bundle (1=Low, 2=Medium, 3=High and 4=Unlimited).
• Period in Months
• Allow international call from package minutes (Y or N)
• Reference Number.
• Monthly Charge (in pence).
• Client Name.
An example of the sort of file I'm looking to write would be this.
So far I have this code
public void appendFile()
{
PrintWriter output = null;
File confidential = new File("contracts.txt");
try
{
// create new file
FileWriter fw = new FileWriter(confidential, true);
output = new PrintWriter(fw);
}
catch (FileNotFoundException e) // Problem with file
{
System.out.println("Error: Problem creating the file! Program closing");
System.exit(0);
}
catch (IOException ex)
{
System.out.println("Error: Problem creating the file! Program closing");
System.exit(0);
}
output.print(date);
output.print(" ");
output.print(minutes);
output.print(" ");
output.print(data);
output.print(" ");
output.print(length);
output.print(" ");
output.print(international);
output.print(" ");
output.print(ref);
output.print(" ");
output.print(price);
output.print(" ");
output.print(name);
output.close();
}// end of main`
As you can see, my code is probably wrong and I'm not sure whether or not this method should even be in a seperate class or just stick the code for it in the main class? I appreciate the time it takes to go through my question and I hope someone can help me, as I'm currently about to rip my hair out in frustration of not understanding how to do it.