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(String fileName, String text, String characterEncoding)
This will write a string to a file with the specified encoding
FileOutputStream fos;
byte[] buf = text.getBytes(characterEncoding);
int numWritten;
fos = new FileOutputStream(fileName);
numWritten = 0;
fos.write(buf, numWritten, buf.length);
fos.close();
voidwriteFile(String fileName, String textToSave)
write File
if (fileName == null)
    throw new IOException("filename is null");
if (textToSave == null)
    throw new IOException("textToSave is null");
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
fw.write(textToSave);
bw.close();
...
booleanwriteFile(String fileName, String[] contents)
description writes the array of strings into the file name speecified
BufferedWriter bufWriter = null;
String line;
boolean success = true;
if (null == contents) {
    return false;
try {
    bufWriter = new BufferedWriter(new FileWriter(fileName));
...
voidwriteFile(String fileName, String[] lines)
write File
try {
    File fout = new File(fileName);
    FileOutputStream fos = new FileOutputStream(fout);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    for (String line : lines) {
        osw.write(line);
    osw.close();
...
voidwriteFile(String filename, StringBuilder sb)
write File
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename)));
bw.write(sb.toString());
bw.close();
voidwriteFile(String filePath, String content)
write File
File file = new File(filePath);
synchronized (file) {
    FileOutputStream fos = new FileOutputStream(filePath);
    fos.write(content.getBytes("UTF-8"));
    fos.close();
voidwriteFile(String filePath, String content)
write File
RandomAccessFile raf = new RandomAccessFile(new File(filePath), "rw");
raf.writeUTF(content);
raf.close();
voidwriteFile(String filePath, String content)
Write content to file.
try {
    FileOutputStream os = new FileOutputStream(filePath);
    FileDescriptor fd = os.getFD();
    os.write(content.getBytes());
    os.flush();
    fd.sync();
} catch (IOException e) {
    e.printStackTrace();
...
voidwriteFile(String filePath, String content)
ananya.tools.utils.FileUtils method to write string content to a file
writeFile(new File(filePath), content);
booleanWriteFile(String filepath, String content)
Write File
File outputFile = new File(filepath);
if (outputFile.exists()) {
    outputFile.delete();
File parentDirectory = outputFile.getParentFile();
if (parentDirectory != null && !parentDirectory.exists()) {
    parentDirectory.mkdirs();
BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new FileWriter(filepath));
    writer.write(content);
    return true;
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
return false;