Appending to a text file Learn programming Java

Lesson:

File Management


Exercise:

Appending to a text file 16


Objetive:

Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt". If the file exists, the new content must be appended to its end.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		java.io.OutputStreamWriter myFile = File.AppendText("file.txt");
		String line;

		do
		{
			System.out.print("Enter a sentence: ");
			line = new Scanner(System.in).nextLine();

			if (!line.equals(""))
			{
				myFile.write(line + System.lineSeparator());
			}
		} while (!line.equals(""));

		myFile.close();
	}
}