Java Utililty Methods Write String to File

List of utility methods to do Write String to File

Description

The list of methods to do Write String to File are organized into topic(s).

Method

voidwriteFile(File file, String contents)
Store the contents of a String in a file.
writeFile(file.getAbsolutePath(), contents);
voidwriteFile(File file, String contents)
write File
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
try {
    writer.write(contents);
} finally {
    writer.close();
voidwriteFile(File file, String data)
Write a String to a file.
PrintWriter out = openForWriting(file);
try {
    out.print(data);
} finally {
    out.close();
voidwriteFile(File file, String data)
write File
try (FileWriter writer = new FileWriter(file)) {
    writer.write(data);
voidwriteFile(File file, String data)
Writes String data to the given path (UTF-8).
writeFile(file, data.getBytes("UTF-8"));
voidwriteFile(File file, String data, boolean append)
write File
try {
    FileOutputStream fos = new FileOutputStream(file, append);
    fos.write(data.getBytes());
    fos.close();
} catch (Exception e) {
booleanwriteFile(File file, String dataString)
Writes a string to a file;
try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    out.print(dataString);
    out.flush();
    out.close();
} catch (IOException e) {
    return false;
return true;
booleanwriteFile(File file, String text)
Write to File
return writeFile(file.toString(), text);
voidwriteFile(File file, String text)
write File
writeFile(file, text, false);
voidwriteFile(File file, String... lines)
write File
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (String line : lines) {
    writer.append(line);
    writer.newLine();
writer.close();