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 string, File file)
write String To File
PrintWriter out = new PrintWriter(file);
out.print(string);
out.close();
voidwriteStringToFile(String string, String fileName)
write String To File
File file = null;
boolean fileCreated = false;
if (fileName == null || string == null) {
    throw new IllegalArgumentException(
            "fileName or string passed in was NULL.  Must have both these values.");
file = new File(fileName);
if (!file.exists()) {
...
voidwriteStringToFile(String string, String fileName, boolean append)
write String To File
BufferedWriter out = new BufferedWriter(new FileWriter(fileName, append));
out.write(string);
out.close();
voidwriteStringToFile(String string, String path)
Writes specified string to specified file path.
FileWriter fileWriter = null;
try {
    File clusterFile = new File(path);
    fileWriter = new FileWriter(clusterFile);
    fileWriter.write(string);
} finally {
    if (fileWriter != null) {
        fileWriter.close();
...
voidwriteStringToFile(String stringContent, String fileName)
* Output string to file ******************************************.
PrintWriter printOut = new PrintWriter("c:\\temp\\" + fileName);
printOut.println(stringContent);
printOut.close();
voidwriteStringToFile(String stringToBeWritten, String filePath)
Creates, respectively overwrites the specified file with a given string
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write(stringToBeWritten);
bufferedWriter.close();
FilewriteStringToFile(String stringToWrite, String fileName)
Writes a string (generated from running the previous method) on a plain text file.
File file = new File(fileName);
FileWriter writer = null;
try {
    writer = new FileWriter(file);
    writer.write(stringToWrite);
    writer.close();
} catch (IOException ex) {
    ex.printStackTrace();
...
voidwriteStringToFile(String text, File file)
(Near-)atomically create or overwrite the specified file with the specified content, encoded as UTF-8.
try {
    File tempFile = File.createTempFile(file.getName(), ".tmp", file.getParentFile());
    FileOutputStream output = new FileOutputStream(tempFile, false);
    OutputStreamWriter writer = new OutputStreamWriter(output);
    writer.write(text);
    writer.flush();
    writer.close();
    if (file.exists())
...
voidwriteStringToFile(String text, String file)
write String To File
OutputStream os = new FileOutputStream(file);
os.write(text.getBytes());
os.close();
voidwriteStringToFile(String text, String filePath)
Writes a string out to a file
File outFile = new File(filePath);
writeStringToFile(text, outFile);