Android 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

voidwriteFileAsString(File file, String text)
write File As String
try {
    ensureWriteable(file);
    OutputStream os = new FileOutputStream(file);
    if (file.getName().endsWith(".gz")) {
        os = new GZIPOutputStream(os);
    PrintStream out = new PrintStream(os);
    out.print(text);
...
voidwriteFileAsString(File file, String text, String encoding)
write File As String
try {
    ensureWriteable(file);
    OutputStream os = new FileOutputStream(file);
    if (file.getName().endsWith(".gz")) {
        os = new GZIPOutputStream(os);
    PrintStream out = new PrintStream(os, true, encoding);
    out.print(text);
...
voidwriteFileAsString(String filename, String text)
write File As String
if (filename.startsWith(SERIALPREFIX)) {
    filename = serialFileName(filename);
writeFileAsString(new File(filename), text);
voidwriteFileAsString(String filename, String text, String encoding)
write File As String
if (filename.startsWith(SERIALPREFIX)) {
    filename = serialFileName(filename);
writeFileAsString(new File(filename), text, encoding);
voidwriteFileAsStringWorldWritable(File file, String text)
write File As String World Writable
ensureWorldWriteable(file);
writeFileAsString(file, text);
file.setWritable(true, false);
voidwriteFileAtomically(File file, String content, String encoding)
Writes a string to a file, creating the directory if necessary.
File parent = file.getParentFile();
if (!parent.exists()) {
    parent.mkdirs();
File tmpFile = File.createTempFile("GSFileUtilWFA", ".tmp", parent);
try {
    Writer out = new OutputStreamWriter(new FileOutputStream(
            tmpFile), encoding);
...
voidwriteFileWithBom(File file, String content, String encoding)
write File With Bom
if (!file.exists()) {
    file.getParentFile().mkdirs();
FileOutputStream out = null;
try {
    out = new FileOutputStream(file);
    writeBom(out, encoding);
    out.write(content.getBytes(encoding));
...
voidwriteLargerTextFile(File file, List aLines)
write Larger Text File
try (BufferedWriter writer = Files.newBufferedWriter(
        Paths.get(file.getPath()), StandardCharsets.UTF_8)) {
    for (String line : aLines) {
        writer.write(line);
        writer.newLine();
voidwriteLineTextFile(File file, String data)
write Line Text File
PrintWriter pw = null;
try {
    pw = new PrintWriter(file);
    pw.print(data);
    pw.flush();
} catch (Exception e) {
    e.printStackTrace();
    try {
...
voidwriteLineTextFile(File file, String[] dataArray)
write Line Text File
PrintWriter pw = null;
try {
    pw = new PrintWriter(file);
    for (String data : dataArray) {
        pw.println(data);
    pw.flush();
} catch (Exception e) {
...