I'm trying to write some code that will write certain variables to a text file and create a simple record system. So when a button is clicked the variables are calculated and written to a text file. How can I set it up so that when the button is clicked again and the variables change due to the new values, the new set of variables are written on a different line in the text file and don't simple overwrite the old ones. So there would be one record per line.

This is the code I have for the writer component of the programme so far:

try
			{
				FileWriter writer = new FileWriter("YachtClubRecords.txt");
				writer.write(outputName);
				writer.write(outputLength);
				writer.write(durationType);
				writer.write(outputCost);
				writer.flush();
				writer.close();
			}

			catch(IOException ioe)
			{
				System.out.println("IO Exception");
			}

At the moment it obviously overwrites the previous record again and again. Thanks.

Recommended Answers

All 4 Replies

Thanks that fixed the original problem. My current code is as follows:

try
			{
				FileWriter writer = new FileWriter("YachtClubRecords.txt", true);
				writer.write("\n" + outputName);
				writer.write(", " + outputLength);
				writer.write(", " + durationType);
				writer.write(", " + outputCost);
				writer.flush();
				writer.close();
			}

			catch(IOException ioe)
			{
				System.out.println("IO Exception");
			}

This is meant to print every record to a separate line. But when I check the text file in notepad all the records are on the same line. Although when I copy and paste it here, they're on separate lines...

And help would be appreciated. Thanks.

That's just notepad showing its age. Use wordpad. Or a decent editor.

I found the problem. Windows doesn't like \n for some reason. \r\n works well.

Thanks for the help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.