Skip to content

Commit

Permalink
savefile static method
Browse files Browse the repository at this point in the history
  • Loading branch information
ttpro1995 committed Sep 14, 2015
1 parent 9539d67 commit 7c04e63
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7c04e63

Please sign in to comment.