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 str, File file)
write String To File
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
char[] chars = str.toCharArray();
out.write(chars, 0, chars.length);
out.close();
FilewriteStringToFile(String str, File file)
write String To File
try {
    BufferedReader in4 = new BufferedReader(new StringReader(str));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    String tempStr = null;
    while ((tempStr = in4.readLine()) != null) {
        out1.println(tempStr);
    out1.close();
...
voidWriteStringToFile(String str, String file)
Write String To File
try {
    BufferedWriter output = new BufferedWriter(new FileWriter(file));
    output.write(str);
    output.flush();
    output.close();
} catch (IOException e) {
    e.printStackTrace();
voidwriteStringToFile(String str, String filePath)
write String To File
PrintWriter tmpASP;
try {
    tmpASP = new PrintWriter(filePath);
    tmpASP.print(str);
    tmpASP.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
voidwriteStringToFile(String str, String filePath)
write String To File
File file = new File(filePath);
try {
    BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
    out.write(str);
    out.close();
} catch (IOException e) {
    e.printStackTrace();
voidwriteStringToFile(String string, File destFile)
Includes workaround for bug: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557
try {
    final FileOutputStream fos = new FileOutputStream(destFile.getAbsoluteFile());
    final BufferedWriter bw = new BufferedWriter(new PrintWriter(fos));
    bw.write(string);
    bw.flush();
    bw.close();
    fos.close(); 
} catch (FileNotFoundException e) {
...
voidwriteStringToFile(String string, File dstFile)
Writes the string to dstFile
try (BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(dstFile));) {
    bufferedwriter.write(string);
} catch (FileNotFoundException e) {
    throw new RuntimeException("Filed does not exist " + dstFile.getAbsolutePath()); 
} catch (IOException e) {
    throw new RuntimeException("Could not write to file " + dstFile.getAbsolutePath()); 
voidwriteStringToFile(String string, File file)
Writes a string to the file at the specified path, overwriting it if it already exists.
try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    writer.write(string);
    writer.close();
} catch (IOException e) {
voidwriteStringToFile(String string, File file)
write String To File
FileWriter fw = new FileWriter(file);
fw.append(string);
fw.close();
FilewriteStringToFile(String string, File file)
write String To File
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
    writer.write(string);
return file;