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

booleanwriteFile(String buffer, String fname)
write File
return writeFile(buffer, fname, "UTF-8", false);
voidwriteFile(String canonicalFilename, String text)
Save the given text to the given filename.
File file = new File(canonicalFilename);
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(text);
out.close();
voidwriteFile(String content, File file)
We write the string specified into a file.
PrintStream outFile = new PrintStream(new FileOutputStream(file));
for (int i = 0; i < content.length(); i++) {
    outFile.print(content.charAt(i));
outFile.flush();
outFile.close();
voidwriteFile(String content, String fileName)
write File
PrintWriter writer = new PrintWriter(fileName);
try {
    writer.print(content);
} finally {
    closeQuietly(writer);
voidwriteFile(String content, String fileName, boolean append)
write File
FileWriter writer = null;
try {
    try {
        writer = new FileWriter(fileName, append);
        writer.write(content);
    } finally {
        writer.close();
} catch (Exception e) {
    throw new RuntimeException(e);
voidwriteFile(String content, String filepath, boolean append)
write File
BufferedWriter writer = null;
File file = new File(filepath);
file.getParentFile().mkdirs();
try {
    writer = new BufferedWriter(new FileWriter(file, append));
    writer.write(content);
} catch (IOException e) {
} finally {
...
voidwriteFile(String content, String path)
It is called to save a file
Writer writer = new BufferedWriter(new FileWriter(new File(path)));
writer.write(content);
writer.close();
voidwriteFile(String content, String path)
write File
File myFile = new File(path);
try {
    myFile.createNewFile();
    FileOutputStream fOut = new FileOutputStream(myFile);
    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
    myOutWriter.append(content);
    myOutWriter.close();
    fOut.close();
...
FilewriteFile(String content, String path)
write the content to file .
File f = new File(path);
if (!f.exists()) {
    try {
        if (f.isFile()) {
            File dir = new File(f.getParent());
            if (!dir.exists())
                dir.mkdirs();
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
BufferedWriter bw = null;
try {
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
    bw.write(content);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (bw != null) {
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
return f;
booleanwriteFile(String content, String targetfileName)
write File
PrintWriter writer = null;
try {
    writer = new PrintWriter(targetfileName);
    writer.write(content);
} catch (IOException ex) {
    ex.printStackTrace();
    return false;
} finally {
...