Java Utililty Methods Text File Write

List of utility methods to do Text File Write

Description

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

Method

voidwriteStringToFile(File file, String data, String encoding)
Writes a String to a file creating the file if it does not exist.
if (encoding == null) {
    throw new IllegalArgumentException("Must specify encoding");
OutputStream out = null;
try {
    out = openOutputStream(file);
    if (data != null) {
        out.write(data.getBytes(encoding));
...
voidwriteStringToFile(File file, String data, String encoding)

Writes a String to a file creating the file if it does not exist.

OutputStream out = new FileOutputStream(file);
try {
    write(data, out, encoding);
} finally {
    closeQuietly(out);
voidwriteStringToFile(File file, String data, String encoding)

Writes data to a file.

OutputStream out = new java.io.FileOutputStream(file);
try {
    out.write(data.getBytes(encoding));
} finally {
    closeQuietly(out);
voidwriteStringToFile(File file, String s)
write String To File
try (PrintStream out = new PrintStream(new FileOutputStream(file, false))) {
    out.print(s);
voidwriteStringToFile(File file, String str, boolean compress)
Write the given String to the File.
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
if (compress) {
    out = new GZIPOutputStream(out);
PrintWriter pout = new PrintWriter(out);
pout.print(str);
pout.flush();
pout.close();
...
voidwriteStringToFile(File file, String string)
write String To File
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
    out.write(string.getBytes());
} finally {
    out.close();
FilewriteStringToFile(File file, String string, String encoding)
write String To File
FileOutputStream out = new FileOutputStream(file);
PrintStream p = new PrintStream(out, true, encoding);
p.print(string);
p.flush();
p.close();
out.close();
return file;
voidwriteStringToFile(File file, String stringToWrite)
write String To File
createNewFile(file);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
    writer.write(stringToWrite);
voidwriteStringToFile(File file, String text)
write String To File
System.out.println("Writing: " + file);
File parentFile = file.getParentFile();
if (parentFile != null && !parentFile.exists()) {
    parentFile.mkdirs();
if (text == null) {
    throw new IllegalArgumentException("Text is null");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
try {
    out.write(text);
} catch (IOException e) {
    throw e;
} finally {
    out.close();
voidwriteStringToFile(File file, String text)
Writes a string into a file.
PrintStream out = new PrintStream(file);
out.print(text);
out.close();