Android Text File Write writeToSDFile(String directory, String file_name, String text)

Here you can find the source of writeToSDFile(String directory, String file_name, String text)

Description

write To SD File

Declaration

public static Boolean writeToSDFile(String directory, String file_name,
            String text) 

Method Source Code

//package com.java2s;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.PrintWriter;

public class Main {
    public static Boolean writeToSDFile(String directory, String file_name,
            String text) {/*from  ww w.j av  a  2  s.com*/

        // Find the root of the external storage.
        // See
        // http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

        File root = Environment.getExternalStorageDirectory();

        // See
        // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

        File dir = new File(root.getAbsolutePath() + "/" + directory);
        dir.mkdirs();
        File file = new File(dir, file_name);

        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println(text);
            pw.flush();
            pw.close();
            f.close();
            // Log.v(TAG, "file written to sd card");
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // Log.i(TAG, "******* File not found. Did you" +
            // " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }
}

Related

  1. writeToFile(final String path, final String content)
  2. saveFile(String content, File file)
  3. writeFileSdcard(File file, String message)
  4. writeFile(String filepath, String text)
  5. writeStringToFile(String text, String filePath)
  6. writeFileEnd(String filepath, String text)
  7. WriteFile(String file, String message)
  8. writeNewFile(String filePath, String fileContents)
  9. writeStringToFile(File file, String data)