Android Text File Write writeStringToFile(String text, String filePath)

Here you can find the source of writeStringToFile(String text, String filePath)

Description

write String To File

Declaration

public static boolean writeStringToFile(String text, String filePath) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

public class Main {
    public static boolean writeStringToFile(String text, String filePath) {
        OutputStream os = null;/*  ww  w .  j ava 2  s .  c o  m*/
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            os = new FileOutputStream(file);
            byte[] data = text.getBytes("UTF-8");
            os.write(data);
            os.flush();
            os.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}

Related

  1. writeToFile(StringBuffer sb, String directory, String fileName)
  2. writeToFile(final String path, final String content)
  3. saveFile(String content, File file)
  4. writeFileSdcard(File file, String message)
  5. writeFile(String filepath, String text)
  6. writeToSDFile(String directory, String file_name, String text)
  7. writeFileEnd(String filepath, String text)
  8. WriteFile(String file, String message)
  9. writeNewFile(String filePath, String fileContents)