The following static methods can be used to easily append text to the end of a file. Different arguments allow a flexible call with path string or file object.
The path method expects for the first argument the file path as string.
public static void append(String path, String text, String charsetName)
throws FileNotFoundException, IOException
{
append(new File(path), text, charsetName);
}
The file method expects for the first argument the file path as file object. Call of the method write saves the text to the end of the file.
public static void append(File file, String text, String charsetName)
throws FileNotFoundException, IOException
{
if (file.exists() == false)
throw new FileNotFoundException("File does not exist! "+file);
write(new FileOutputStream(file,true), text, charsetName);
}
append("test.txt", "test content", "utf-8");
append(file, "test content", "utf-8");
Author: Johannes HammoudComments Date: 22.08.2009