安裝應用的時候拷貝一個DB文件到應用database下

拷貝文件到database下,在res下建立一個raw的文件夾,把要拷貝的數據庫文件放在raw下,而後執行下面代碼就OK了 java


private static String DATABASES_DIR="/data/data/com.tcd.wp/databases";
	private static String DATABASE_NAME="wp.db";
public static void copyDatabaseFile(Context context, boolean isfored) {  
    
    File dir = new File(DATABASES_DIR);  
    if (!dir.exists() || isfored) {  
        try {  
            dir.mkdir();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    File dest = new File(dir, DATABASE_NAME);  
      
    try {  
        if(dest.exists()){  
            dest.delete();  
        }  
        dest.createNewFile();     
        InputStream in = context.getResources().openRawResource(R.raw.wp);  
        int size = in.available();  
        byte buf[] = new byte[size];  
        in.read(buf);  
        in.close();  
        FileOutputStream out = new FileOutputStream(dest);  
        out.write(buf);  
        out.close();  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}
相關文章
相關標籤/搜索