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

voidwriteTextToFile(String file, String text)
Simple method to write text to a file
try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    writer.write(text);
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
voidwriteToFile(File file, String content)
write To File
try {
    FileWriter fstream = new FileWriter(file, true);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(content);
    out.close();
} catch (IOException e) {
    throw new RuntimeException(e);
voidwriteToFile(File target, String s, String charSet)
write To File
FileOutputStream fos = new FileOutputStream(target);
fos.write(s.getBytes(charSet));
fos.close();
voidwriteToFile(String content, String filePath)
write To File
File file = createFile(filePath);
FileWriter fw = new FileWriter(file, true);
PrintWriter pWriter = new PrintWriter(new BufferedWriter(fw));
pWriter.println(content);
pWriter.close();
fw.close();
voidwriteToFile(String sb, String directory, String fileName)
write To File
File dir = new File(directory);
dir.mkdirs();
if (!directory.endsWith("/")) {
    directory = directory + "/";
File out = new File(directory + fileName);
System.out.println(directory + fileName);
try {
...
StringwriteToFile(String textToWrite, String fileName)
writes the contents of textToWrite to a file on the local filesystem identified by fileName.
File outFile = new File(fileName);
try {
    if (!outFile.exists()) {
        outFile.createNewFile();
    FileWriter out = new FileWriter(outFile, true);
    out.write(textToWrite);
    out.close();
...
voidwriteToFile(StringBuffer sb, String directory, String fileName)
write To File
writeToFile(sb.toString(), directory, fileName);
voidwriteToFile(final String path, final String content)
Dumps a string into a file.
final File file = new File(path);
final RandomAccessFile access = new RandomAccessFile(file, "rws");
try {
    access.writeBytes(content);
} finally {
    access.close();
voidsaveFile(String content, File file)
Saves the content String to the specified file.
BufferedWriter writer = Files.newBufferedWriter(file.toPath(),
        CHARSET);
writer.write(content, 0, content.length());
writer.close();
voidwriteFileSdcard(File file, String message)
write File Sdcard
FileOutputStream fout = new FileOutputStream(file);
byte[] bytes = message.getBytes("UTF-8");
fout.write(bytes);
fout.flush();
fout.close();