package com.zhiyou.filetoolstest;
import android.content.Context;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Administrator on 2016/5/11.
*/
public class FileTools {
public static final int SUCCESS = 0;
public static final int FILE_NOT_FONUD = 1;
public static final int READ_WRITE_ERROR = 2;
public static final void debug (Context context) {
File filesDir = context.getFilesDir();
File[] files = filesDir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println("--->"+files[i].getName()+" len="+files[i].length());
}
}
public static final boolean isFileExistsInFiles(Context context, String filename) {
File filesDir = context.getFilesDir();
File file = new File(filesDir, filename);
return file.exists();
}
public static final boolean deleteFileFromFiles(Context context, String filename) {
File filesDir = context.getFilesDir();
File file = new File(filesDir, filename);
return file.delete();
}
public static final boolean copyFileFromRawToFiles(Context context, int raw_id, String filename) {
InputStream in = null;
OutputStream out = null;
boolean result = false;
try {
in = context.getResources().openRawResource(raw_id);
out = context.openFileOutput(filename, Context.MODE_PRIVATE);
byte[] buffer = new byte[in.available()];
in.read(buffer);
out.write(buffer);
out.flush();
result = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
result = false;
} catch (IOException e) {
e.printStackTrace();
result = false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
}
}
return result;
}
}