From 7c04e63d77f489473ee6f959529e2fdd305c1a91 Mon Sep 17 00:00:00 2001 From: "thien.thai" Date: Mon, 14 Sep 2015 11:43:27 +0700 Subject: [PATCH] savefile static method --- .../hahattpro/meowdebughelper/SaveFile.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/meowdebughelper/src/main/java/com/hahattpro/meowdebughelper/SaveFile.java b/meowdebughelper/src/main/java/com/hahattpro/meowdebughelper/SaveFile.java index a173889..8bd2773 100644 --- a/meowdebughelper/src/main/java/com/hahattpro/meowdebughelper/SaveFile.java +++ b/meowdebughelper/src/main/java/com/hahattpro/meowdebughelper/SaveFile.java @@ -8,6 +8,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -47,6 +49,59 @@ public SaveFile(String NAME, String file_body, Context appContext) { Log.i("SaveFile path","path : "+path); } + public static File save(String NAME, String file_body) { + String FILE_NAME =NAME; + + String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/" +FILE_NAME; + File file = null; + try{ + file = new File(path); + FileOutputStream out = new FileOutputStream(file); + OutputStreamWriter writer = new OutputStreamWriter(out); + writer.write(file_body); + writer.close(); + out.close(); + } + catch (FileNotFoundException e){ + + } + catch (IOException e){ + //do nothing + } + Log.i("SaveFile path","path : "+path); + return file; + } + + private static File save(String NAME, InputStream file_body){ + File file= null; + + try{ + file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),NAME); + copy(file_body, file); + } + catch (FileNotFoundException e){ + + } + catch (IOException e){ + //do nothing + } + return file; + } + + public static void copy(InputStream in, File dst) throws IOException { + + OutputStream out = new FileOutputStream(dst); + + // Transfer bytes from in to out + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + in.close(); + out.close(); + } + public File getFile() {//getter return file;