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 path, String content)
Write the given string content into a file with the specified path.
writeStringToFile(new File(path), content);
voidwriteStringToFile(String path, String contents)
write String To File
writeStringToFile(new File(path), contents, "UTF-8");
voidwriteStringToFile(String s, File f)
Writes a string to a file replacing the entire contents
BufferedWriter bw = null;
try {
    bw = new BufferedWriter(new FileWriter(f));
    bw.write(s);
} finally {
    if (null != bw) {
        bw.close();
voidwriteStringToFile(String s, File f)
writes String as content of a java File object to disk.
if (f == null)
    throw new IOException("null file");
FileOutputStream fos = new FileOutputStream(f);
byte[] data = s.getBytes();
fos.write(data);
fos.close();
voidwriteStringToFile(String s, File f)
write String To File
if (f.exists()) {
    if (!f.delete()) {
        throw new RuntimeException("Cannot delete file " + f.getAbsolutePath());
FileWriter fw = null;
try {
    if (!f.createNewFile()) {
...
voidwriteStringToFile(String s, File file)
write String To File
OutputStream out = null;
try {
    out = new FileOutputStream(file);
    out.write(s.getBytes());
} finally {
    forceClosed(out);
voidwriteStringToFile(String s, String filename)
writeStringToFile Write a string, which may include newline characters, line by line to the file specified by filename.
String inputStr = s.trim();
if (inputStr.length() == 0)
    throw new IOException("Input string contains no printable characters.");
BufferedReader br = new BufferedReader(new StringReader(inputStr));
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
try {
    String line;
    while ((line = br.readLine()) != null) {
...
voidwriteStringToFile(String s, String filePathname)
write String To File
makeParentDirectories(filePathname);
BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new FileWriter(filePathname));
    writer.write(s);
} finally {
    if (writer != null) {
        writer.close();
...
booleanwriteStringToFile(String sdat, File f)
write String To File
String fnm = f.getName();
boolean ok = false;
if (f != null) {
    boolean dogz = (fnm.endsWith(".gz"));
    try {
        OutputStream fos = new FileOutputStream(f);
        if (dogz) {
            fos = new GZIPOutputStream(fos);
...
voidwriteStringToFile(String str, File file)
write String To File
BufferedWriter writer = null;
try {
    file.delete();
    file.createNewFile();
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(str);
} catch (IOException e) {
    e.printStackTrace();
...