android下,如果将项目中assets目录下的文件转换为File对象呢?废话不多说,直接上代码。
import android.app.Activity;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtils {
public static final String BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/game/storyprogrammer";
/** 临时目录*/
public static final String BASE_TEMP_PATH = BASE_PATH + "/temp";
private Activity activity;
public FileUtils(Activity context) {
activity = context;
}
/**
* 复制assets下的文件时用这个方法
* @throws IOException
*/
public void copyBigDataBase(String assetName, String savepath, String savename) throws IOException {
InputStream myInput;
File dir = new File(savepath);
if (!dir.exists()) {
dir.mkdirs();
}
File dbf = new File(savepath + savename);
if (dbf.exists()) {
dbf.delete();
}
String outFileName = savepath + savename;
OutputStream myOutput = new FileOutputStream(outFileName);
myInput = activity.getAssets().open(assetName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
}
注意:本文归作者所有,未经作者允许,不得转载
原文地址: http://blog.wsmee.com/post/46
版权声明:非商用-非衍生-保持署名| Creative Commons BY-NC-ND 3.0