/**
 * //[Android] Copy File
 * //http://fecbob.pixnet.net/blog/post/36060051-%5Bandroid%5D-copy-file
 */
package dolphin.android.util;

import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
    public static boolean copyFile(File source, File dest) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(source));
            bos = new BufferedOutputStream(new FileOutputStream(dest, false));

            byte[] buf = new byte[1024];
            bis.read(buf);

            do {
                bos.write(buf);
            } while (bis.read(buf) != -1);
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                return false;
            }
        }

        return true;
    }

    // WARNING ! Inefficient if source and dest are on the same filesystem !
    public static boolean moveFile(File source, File dest) {
        return copyFile(source, dest) && source.delete();
    }

    // Returns true if the sdcard is mounted rw
    public static boolean isSDMounted() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * write string content to a file
     * @param dst
     * @param content
     * @return
     */
    public static boolean writeStringToFile(File dst, String content) {
        try {//http://stackoverflow.com/a/1053474
            BufferedWriter writer = new BufferedWriter(new FileWriter(dst));
            writer.write(content, 0, content.length());
            writer.close();
            writer = null;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * read the file content to string
     * @param src
     * @return
     */
    public static String readFileToString(File src) {
        StringBuilder sb = new StringBuilder();
        try {
            FileReader sr = new FileReader(src);
            int len = 0;
            while (true) {//read from buffer
                char[] buffer = new char[1024];
                len = sr.read(buffer);//, size, 512);
                //Log.d(TAG, String.format("%d", len));
                if (len > 0) {
                    sb.append(buffer);
                } else {
                    break;
                }
            }
            //Log.i(TAG, String.format("  length = %d", sb.length()));
            sr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString().trim();
    }
}

@@ -167,16 +167,42 @@
 onment;%0D
+%0Aimport android.util.Log;%0D
 %0A%0D%0Aimpor
@@ -540,24 +540,77 @@
 ileUtils %7B%0D%0A
+    public final static String TAG = %22FileUtils%22;%0D%0A%0D%0A
     public s
@@ -651,32 +651,196 @@
 , File dest) %7B%0D%0A
+        if (source == null %7C%7C !source.exists()) %7B%0D%0A            Log.e(TAG, %22no source: %22 + source);%0D%0A            return false;//no file source to copy%0D%0A        %7D%0D%0A%0D%0A
         Buffered
@@ -1272,32 +1272,88 @@
 Exception e) %7B%0D%0A
+            Log.e(TAG, %22copyFile: %22 + e.getMessage());%0D%0A
             retu
@@ -1528,32 +1528,92 @@
 Exception e) %7B%0D%0A
+                Log.e(TAG, %22copyFile: %22 + e.getMessage());%0D%0A
                 
@@ -2132,16 +2132,24 @@
 a file%0D%0A
+     *%0D%0A
      * @
@@ -2276,24 +2276,156 @@
 content) %7B%0D%0A
+        if (content == null) %7B%0D%0A            Log.e(TAG, %22no content%22);%0D%0A            return false;//no content to write%0D%0A        %7D%0D%0A%0D%0A
         try 
@@ -2726,32 +2726,97 @@
 tStackTrace();%0D%0A
+            Log.e(TAG, %22writeStringToFile: %22 + e.getMessage());%0D%0A
             retu
@@ -2887,32 +2887,97 @@
 tStackTrace();%0D%0A
+            Log.e(TAG, %22writeStringToFile: %22 + e.getMessage());%0D%0A
             retu
@@ -3078,16 +3078,24 @@
 string%0D%0A
+     *%0D%0A
      * @
@@ -3181,24 +3181,178 @@
 ile src) %7B%0D%0A
+        if (src == null %7C%7C !src.exists()) %7B%0D%0A            Log.e(TAG, %22no source: %22 + src);%0D%0A            return null;//no file source to read%0D%0A        %7D%0D%0A%0D%0A
         Stri
@@ -3970,32 +3970,32 @@
 Exception e) %7B%0D%0A
-
             e.pr
@@ -4004,32 +4004,96 @@
 tStackTrace();%0D%0A
+            Log.e(TAG, %22readFileToString: %22 + e.getMessage());%0D%0A
         %7D%0D%0A     

add more check before actions