Example #1
0
 /**
  * 根据文件路径 递归创建文件
  *
  * @param file
  */
 public static void createDipPath(String file) {
   String parentFile = file.substring(0, file.lastIndexOf("/"));
   File file1 = new File(file);
   File parent = new File(parentFile);
   if (!file1.exists()) {
     parent.mkdirs();
     try {
       file1.createNewFile();
       AppLog.i(TAG, "Create new file :" + file);
     } catch (IOException e) {
       AppLog.e(TAG, e.getMessage());
     }
   }
 }
  /**
   * Exec SQL from file in assets
   *
   * @param filename
   * @param db
   */
  protected void execSQLFile(String filename, SQLiteDatabase db) {
    // get SQL
    String sql = getSQLFromFile(filename);

    // check sql
    if (sql == null || sql.length() == 0) return;

    // split sql queries
    String[] queries = sql.split(";");

    // begin transaction
    db.beginTransaction();

    try {
      // iteratate and exec queries
      for (String query : queries) {
        db.execSQL(query);
      }

      // set transaction successful
      db.setTransactionSuccessful();
    } catch (Exception e) {
      AppLog.e(e);
    } finally {
      // end transaction
      db.endTransaction();
    }
  }
  /**
   * Get SQL string from assets
   *
   * @param filename
   * @return
   */
  protected String getSQLFromFile(String filename) {
    try {
      // open input stream from assets
      java.io.InputStream is = mContext.getAssets().open(filename);
      int size = is.available();

      // Read the entire asset into a local byte buffer.
      byte[] buffer = new byte[size];
      is.read(buffer);
      is.close();

      // Convert the buffer into a string.
      return new String(buffer);
    } catch (Exception e) {
      AppLog.e(e);
    }

    return null;
  }