Android Text File Write writeTextFile(String string, String fileName)

Here you can find the source of writeTextFile(String string, String fileName)

Description

write text to a specified text file store in temporary path

Parameter

Parameter Description
string text to be written
fileName file name specified

Return

text file contains the text

Declaration

public static File writeTextFile(String string, String fileName) 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main{
    public static final String tmpdir = System
            .getProperty("java.io.tmpdir") + File.separator;
    /**/* w  w w .  j av  a 2 s  . c  om*/
     * write text to a specified text file store in temporary path
     * @param string text to be written
     * @param fileName file name specified
     * @return text file contains the text
     */
    public static File writeTextFile(String string, String fileName) {
        try {
            File file = new File(FileUtil.tmpdir + fileName + ".txt");
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(string);
            osw.flush();
            osw.close();
            fos.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. writeStringToFile(String string, File file)
  2. writeStringToFile(String stringToWrite, String filename)
  3. writeText(File file, String text)
  4. writeText(File file, String text, String charsetName)
  5. writeTextFile(String path, String text)
  6. writeTextToFile(String file, String text)
  7. writeToFile(File file, String content)
  8. writeToFile(File target, String s, String charSet)
  9. writeToFile(String content, String filePath)