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(String f, String s, boolean append)
Write a string to file - CSV Format
try {
    String filename = f;
    FileWriter fw = new FileWriter(filename, append);
    fw.write(s);
    fw.write("\n");
    fw.close();
} catch (IOException io) {
    System.err.println("IOException: " + io.getMessage());
...
voidwriteStringToFile(String file, String data, boolean append)
Write string to file.
FileWriter fstream;
try {
    fstream = new FileWriter(file, append);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(data);
    out.close();
} catch (IOException e) {
    e.printStackTrace();
...
voidwriteStringToFile(String file, String str, boolean append, boolean newLine)
Write/append a string to a file.
try {
    FileWriter fw = new FileWriter(file, append);
    fw.write(str);
    if (newLine) {
        fw.write("\n");
    fw.close();
} catch (IOException ioe) {
...
voidwriteStringToFile(String fileName, String content)
write String To File
FileWriter fileWriter = null;
try {
    File newTextFile = new File(fileName);
    fileWriter = new FileWriter(newTextFile);
    fileWriter.write(content);
    fileWriter.close();
} catch (IOException ex) {
    throw new RuntimeException(ex);
...
voidwriteStringToFile(String filename, String content)
write String To File
File file = new File(filename);
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.print(content);
pw.close();
booleanwriteStringToFile(String fileName, String src)
write String To File
if (src == null) {
    src = "";
boolean isOk = true;
FileWriter out = null;
try {
    out = new FileWriter(fileName);
    out.write(src);
...
voidwriteStringToFile(String filename, String str)
write String To File
writeStringToFile(filename, str, false);
voidwriteStringToFile(String fileName, String str, boolean isAppend)
write String To File
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, isAppend)));
try {
    bw.write(str);
    bw.flush();
} finally {
    bw.close();
voidwriteStringToFile(String filename, String string)
Writes a string to a file.
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(string);
out.close();
voidwriteStringToFile(String filename, String string, boolean overwrite)
write String To File
File destFile = new File(filename);
try {
    if (destFile.exists()) {
        if (overwrite)
            destFile.delete();
        else
            return;
    destFile.createNewFile();
    Writer outputStream = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"));
    outputStream.write(string);
    outputStream.close();
} catch (IOException e) {
    e.printStackTrace();