askIT

Write to text file

The class FileWriter allows you to write to a text file. Using the option append=true will append data, and not overwrite it.

public static void writeToTextFile(String filePath, String data) {
    try(FileWriter fw = new FileWriter(filePath, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw))
    {
        out.println(data);
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
} // writeToTextFile

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.