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 content)
write File
FileWriter writer = null;
System.out.println("Writing " + fileName);
try {
    writer = new FileWriter(fileName);
    writer.write(content);
} catch (IOException e) {
    e.printStackTrace();
} finally {
...
voidwriteFile(String filename, String content)
Writes a files with a certain path with content.
try {
    FileOutputStream fos = new FileOutputStream(filename);
    fos.write(content.getBytes());
    fos.close();
} catch (Exception e) {
    System.err.println(e);
    e.printStackTrace();
voidwriteFile(String filename, String content)
write File
writeFile(new File(filename), content);
FilewriteFile(String filename, String content)
Writes content to file, in UTF-8 encoding.
PrintWriter pw = null;
try {
    pw = new PrintWriter(filename, FILE_ENCODING);
    pw.write(content);
    pw.flush();
} catch (FileNotFoundException e) {
    throw new IllegalArgumentException("Unable to write to: " + filename, e);
} catch (UnsupportedEncodingException e) {
...
voidWriteFile(String filename, String content)
Write File
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
writer.write(content);
writer.close();
System.out.println("[" + createdFiles + "] Created: " + filename);
createdFiles++;
voidwriteFile(String fileName, String content)
write File
try {
    FileWriter fw = new FileWriter(fileName, true);
    fw.write(content + "\n");
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
voidwriteFile(String filename, String content)
write File
File file = new File(filename);
if (!file.exists()) {
    file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(fos, "UTF-8");
...
voidwriteFile(String filename, String content)
write File
try {
    Writer wrtiter = new FileWriter(filename);
    ;
    BufferedWriter bufferedWriter = new BufferedWriter(wrtiter);
    bufferedWriter.write(content);
    bufferedWriter.close();
} catch (IOException e) {
voidwriteFile(String fileName, String content, String encoding)
write File
File f = new File(fileName);
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw;
if (encoding != null) {
    osw = new OutputStreamWriter(fos, encoding);
} else {
    osw = new OutputStreamWriter(fos);
try {
    osw.write(content);
} finally {
    osw.close();
voidwriteFile(String filename, String contents)
Write out a file to the filename specified.
writeFile(new File(filename), contents);