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 contents)
write File
PrintWriter out = new PrintWriter(fileName, "UTF-8");
out.write(contents);
out.close();
voidwriteFile(String fileName, String contents)
write File
writeFile(fileName, new StringInputStream(contents));
voidwriteFile(String fileName, String contentStr, String charset)
write File
byte[] content = contentStr.getBytes(charset);
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(content);
fos.close();
voidwriteFile(String filename, String data)
write File
if (filename == null || filename.isEmpty() || filename.equals("-")) {
    System.out.println(data);
} else {
    filename = expandUserHome(filename);
    File file = new File(filename);
    try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
        writer.println(data);
voidwriteFile(String fileName, String data)
write File
FileOutputStream fos;
fos = new FileOutputStream(fileName);
fos.write(data.getBytes());
fos.flush();
fos.close();
booleanwriteFile(String FileName, String data)
{ method
try {
    PrintWriter out = openPrintWriter(FileName);
    if (out != null) {
        out.print(data);
        out.close();
        return (true);
    return (false);
...
voidwriteFile(String fileName, String encoding, String content)
write File
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), encoding));
out.append(content);
out.flush();
out.close();
voidwriteFile(String fileName, String fileContent)
Writes the content of the string fileContent to a file.
File file = new File(fileName);
FileWriter writer = new FileWriter(file);
writer.write(fileContent);
writer.close();
voidwriteFile(String fileName, String fileData)
Method writeFile.
writeFile(fileName, fileData, false);
voidwriteFile(String fileName, String message)
write File
try {
    File file = new File(fileName);
    if (!file.exists()) {
        file.createNewFile();
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(message);
...