android处理资源文件复制到database区域java
16lz
2021-01-23
以下是处理过程的一个类,名字起得不好,凑合看着使用吧,呵呵。
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;final public class CopyRawtodata {public static boolean CopyRawtodata(String path,String DbName,Context context,int id,boolean IsRawData){boolean dbExist=checkDataBase(path,DbName);if(!dbExist){ try{ copyDataBase(path,DbName,context,id,IsRawData); }catch(IOException e){ throw new Error("Error copying database"); }}return true;}private static boolean checkDataBase(String path,String DbName){ SQLiteDatabase checkDB = null; try{ String databaseFilename = path+"/"+DbName; checkDB =SQLiteDatabase.openDatabase(databaseFilename, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ } if(checkDB!=null){ checkDB.close(); } return checkDB !=null?true:false;}private static void copyDataBase(String path,String DbName,Context context,int ResId,boolean IsRawData) throws IOException{ String databaseFilenames =path+"/"+DbName; InputStream is; File dir = new File(path+"/"); if(!dir.exists())//判断文件夹是否存在,不存在就新建一个 dir.mkdir(); FileOutputStream os = null; try{ os = new FileOutputStream(databaseFilenames);//得到数据库文件的写入流 }catch(FileNotFoundException e){ e.printStackTrace(); } if(IsRawData) { is = context.getResources().openRawResource(ResId);//得到数据库文件的数据流 } else { is = context.getResources().getAssets().open(DbName);//得到数据库文件的数据流 } byte[] buffer = new byte[8192]; int count = 0; try{ while((count=is.read(buffer))>0){ os.write(buffer, 0, count); os.flush(); } }catch(IOException e){ } try{ is.close(); os.close(); }catch(IOException e){ e.printStackTrace(); } }}
更多相关文章
- Android 建立文件夹、生成文件并写入文本文件内容
- 基于xml类型的压缩数据流的android获取天气的方法
- android下创建文件夹和修改其权限的方法
- 把android assets文件夹内的文件存储到sd卡中
- 系统关闭GPRS数据流量和 GPS的方法
- android保存Bitmap图片到指定文件夹示例
- java拷贝文件夹和android设置文件权限