File Append

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.

Path String

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); }

File Object

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); }

Call

append("test.txt", "test content", "utf-8"); append(file, "test content", "utf-8");

Resources

Author: Johannes HammoudComments Date: 22.08.2009

Language German

Java JSF XHTML WML JavaScript GlassFish MySQL NetBeans

Imprint

Visits
582379
Today
35