Write the database files in raw to data, rawdata
Package com. city. list. db; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import com. city. list. main. r; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. OS. environment;/*** write the database files in raw to the data database * @ author sy **/public class DBManager {private final int BUFFER_SIZ E = 400000; private static final String PACKAGE_NAME = "com. city. list. main "; public static final String DB_NAME =" china_city_name.db "; public static final String DB_PATH ="/data "+ Environment. getDataDirectory (). getAbsolutePath () + "/" + PACKAGE_NAME; // storage path private Context mContext; private SQLiteDatabase database; public DBManager (Context context) {this. mContext = context;}/*** called Method */ Public void openDateBase () {this. database = this. openDateBase (DB_PATH + "/" + DB_NAME);}/*** open the database ** @ param dbFile * @ return SQLiteDatabase * @ author sy */private SQLiteDatabase openDateBase (String dbFile) {File file = new File (dbFile); if (! File. exists () {// open the database file in raw and obtain stream InputStream stream = this. mContext. getResources (). openRawResource (R. raw. china_city_name); try {// write the obtained stream to the data path FileOutputStream outputStream = new FileOutputStream (dbFile); byte [] buffer = new byte [BUFFER_SIZE]; int count = 0; while (count = stream. read (buffer)> 0) {outputStream. write (buffer, 0, count);} outputStream. close (); stream. close (); SQLiteDatabase db = SQLiteDatabase. openOrCreateDatabase (dbFile, null); return db;} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} return database;} public void closeDatabase () {if (database! = Null & database. isOpen () {this. database. close ();}}}