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(CharSequence contents, String filename)
writes string contents to file file
writeStringToFile(contents, new File(filename));
booleanwriteStringToFile(File f, String content, boolean append)
write String To File
PrintWriter fout = null;
try {
    fout = new PrintWriter(new BufferedWriter(new FileWriter(f, append)));
    fout.print(content);
    fout.flush();
    fout.close();
    return true;
} catch (FileNotFoundException e) {
...
voidwriteStringToFile(File f, String s)
Writes a string to a file
FileOutputStream fos = new FileOutputStream(f);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, DEFAULT_FILE_ENCODING));
bw.write(s);
bw.close();
booleanwriteStringToFile(File f, String s)
write String To File
OutputStream out = null;
try {
    out = new FileOutputStream(f);
    out.write(s.getBytes(charset));
    return true;
} catch (IOException e) {
    e.printStackTrace();
} finally {
...
voidwriteStringToFile(File f, String s)
write String To File
BufferedWriter writer = new BufferedWriter(new FileWriter(f));
writer.write(s);
writer.close();
voidwriteStringToFile(File file, String content)
write String To File
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
writer.write(content);
writer.close();
voidwriteStringToFile(File file, String content)
write String To File
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
stream.write(content.getBytes());
stream.close();
booleanwriteStringToFile(File file, String content)
write String To File
try {
    Writer output = new BufferedWriter(new FileWriter(file, false));
    output.write(content);
    output.close();
} catch (IOException e) {
    e.printStackTrace();
    return false;
return true;
voidwriteStringToFile(File file, String contents)
Stores a string into an ordinary workspace file in UTF8 format.
byte[] data = contents.getBytes("UTF8"); 
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
    for (byte b : data) {
        out.write(b);
} finally {
    try {
...
voidwriteStringToFile(File file, String data, String encoding)
write String To File
OutputStream os = null;
try {
    os = new FileOutputStream(file);
    os.write(data.getBytes(encoding));
} finally {
    if (null != os) {
        os.close();