package dev.kkorolyov.simplelogs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Prints a message to a file.
 */
public class FilePrinter implements Printer {
	private File file;
	private BufferedWriter writer;
	
	/**
	 * Creates a new file printer printing to a file of the specified name.
	 * @param filename name of file to print to
	 */
	public FilePrinter(String filename) {
		this(new File(filename));
	}
	/**
	 * Creates a new file printer printing to the specified file.
	 * @param file file to print to
	 */
	public FilePrinter(File file) {
		this.file = file;

		clearFile(this.file);
	}
	
	private static void clearFile(File file) {
		try (FileWriter writer = new FileWriter(file)){
			// Do nothing
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void print(String message) {
		try {
			writer = new BufferedWriter(new FileWriter(file, true));
			
			writer.write(message);
			writer.newLine();
			writer.flush();
			
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -220,17 +220,58 @@
 rinter %7B
+%09// TODO Avoid overwriting existing files
 %0A
-
 %09private
@@ -394,16 +394,101 @@
 d name.%0A
+%09 * If a file of the specified name already exists, all of its data will be cleared!%0A
 %09 * @par
@@ -600,24 +600,24 @@
 ));%0A%09%7D%0A%09/**%0A
-
 %09 * Creates 
@@ -667,16 +667,101 @@
 d file.%0A
+%09 * If a file of the specified name already exists, all of its data will be cleared!%0A
 %09 * @par

Update FilePrinter javadoc