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

voidwriteString(final ObjectOutput out, final String s)
write String
final int len = s.length();
out.writeInt(len);
for (int i = 0; i < len; i++) {
    int v = s.charAt(i);
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
intwriteString(final String data, final int length, final byte[] destination, int offset)
write String
requireDestinationLength(destination, offset, length);
byte[] src;
try {
    src = data.getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
    System.err.println(uee + "\nSwitching to default encoding");
    src = data.getBytes();
for (int i = 0; i < length; ++i) {
    destination[offset + i] = i < src.length - 1 ? src[i] : 0;
return length;
voidwriteString(final String output, final String fileName)
Writes string to file
PrintWriter out = new PrintWriter(fileName, "US-ASCII");
out.print(output);
out.close();
voidwriteString(final String s, final File f)
Writes the specified string to the specified file.
assureParentsExist(f);
PrintWriter pw = null;
try {
    pw = new PrintWriter(f);
    pw.write(s);
} catch (final IOException e) {
    throw new RuntimeException(e);
} finally {
...
booleanwriteString(String a, String MyFilePath)
write String
boolean tag = true;
try {
    FileWriter writer = new FileWriter(MyFilePath, true);
    BufferedWriter bufWriter = new BufferedWriter(writer);
    bufWriter.write(a);
    bufWriter.newLine();
    bufWriter.close();
    writer.close();
...
voidwriteString(String content, String path, String charset)
write String
PrintWriter writer = null;
try {
    writer = getPrintWriter(path, charset, false);
    writer.print(content);
} finally {
    close(writer);
voidwriteString(String contents, File file)
write String
FileWriter out = new FileWriter(file);
try {
    out.write(contents);
} finally {
    out.close();
voidwriteString(String contents, File file)
write String
FileWriter out = new FileWriter(file);
try {
    out.write(contents);
} finally {
    out.close();
booleanwriteString(String data, File file)
Writes a String to disk.
try {
    PrintWriter out = new PrintWriter(new FileWriter(file));
    out.print(data);
    out.close();
    return true;
} catch (IOException e) {
    System.out.println("Problem writing String to disk!");
    e.printStackTrace();
...
voidwriteString(String data, String file_path)
write String
try {
    FileOutputStream out_s = new FileOutputStream(file_path);
    out_s.write(data.getBytes());
    out_s.close();
} catch (Exception e) {
    e.printStackTrace();