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... lines)
write File
PrintStream out = new PrintStream(new FileOutputStream(file));
for (String line : lines)
    out.println(line);
out.flush();
out.close();
voidwriteFile(File file, StringBuffer buffer)
write File
writeFile(file, buffer.toString());
voidwriteFile(File filename, ArrayList lines)
writes ArrayList into file.
try {
    FileWriter fw = new FileWriter(filename);
    for (int i = 0; i < lines.size(); i++)
        fw.write((String) lines.get(i) + "\n");
    fw.close();
} catch (IOException e) {
    System.err.println("error writing file");
    e.printStackTrace();
...
FilewriteFile(final File file, final String content)
write File
try {
    if (file.exists()) {
        file.delete();
    final File parent = file.getParentFile();
    if (!parent.exists()) {
        parent.mkdirs();
    final FileWriter fw = new FileWriter(file);
    fw.write(content);
    fw.close();
    return file;
} catch (final IOException e) {
    throw new RuntimeException("Error in tests!", e);
voidwriteFile(final File file, final String contents)
write File
final File dir = file.getParentFile();
if (dir != null && !dir.isDirectory() && !dir.mkdirs())
    throw new IOException("Could not make " + dir);
final FileWriter writer = new FileWriter(file);
writer.write(contents);
writer.close();
voidwriteFile(final File file, final String contents)
write File
final File result = new File(file.getAbsoluteFile().getParentFile(), file.getName() + ".new");
final FileOutputStream out = new FileOutputStream(result);
out.write(contents.getBytes());
out.close();
result.renameTo(file);
voidwriteFile(final File iFile, final String iContent)
write File
final FileOutputStream fos = new FileOutputStream(iFile);
try {
    final OutputStreamWriter os = new OutputStreamWriter(fos);
    try {
        final BufferedWriter writer = new BufferedWriter(os);
        try {
            writer.write(iContent);
        } finally {
...
voidwriteFile(String absoluteFileName, String content, boolean append)
Writes a string to file
FileWriter fw = null;
Writer writer = null;
try {
    fw = new FileWriter(absoluteFileName, append);
    writer = new BufferedWriter(fw);
    writer.write(content);
} finally {
    if (writer != null)
...
voidwriteFile(String aContent, String aFileName)
This function takes a String with some content and a String which is the filename, writes the content to the file using UTF-8 encoding and finally closes the file.
BufferedWriter fr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(aFileName), "UTF8"));
fr.write(aContent);
fr.close();
booleanwriteFile(String aInFilePath, String aInContent)
write File
boolean lStatus = false;
try {
    FileWriter lWriter = new FileWriter(aInFilePath);
    lWriter.append(aInContent);
    lWriter.flush();
    lWriter.close();
    lStatus = true;
} catch (Exception ex) {
...