Summary of using assets to read files in Android

In the process of doing easyEnglishLearning today, because I want to read the local sql script and place it in the assets folder under the Android directory, I have been reading it all night and a FileNotFound error occurred, and finally I figured it out.

The assets folder is not created by default in Android Studio, which is different from Eclipse, so we have to manually create this folder. At first, I created the assets folder by right-clicking ->new->Directory, who knows that this will not be acquired by the AS compiler. The correct way to create it is to right-click on the app folder and select new->Floder->Assets Floder, so that the created assets folder will be correctly read by AS.

Here is the program when reading:

try {
    System.out.println("路径:"+Configuration.DB_PATH + "/" + schemaName);
    in = new BufferedReader(new InputStreamReader(mContext.getAssets()
            .open(Configuration.DB_PATH + "/" + schemaName)));
    String line;
    String buffer = "";
    while ((line = in.readLine()) != null) {
        buffer += line;
        if (line.trim().endsWith(";")) {
            db.execSQL(buffer.replace(";", ""));
            buffer = "";
        }
    }
} catch (IOException e) {
    Log.e("db-error", e.toString());
} finally {
    try {
        if (in != null)
            in.close();
    } catch (IOException e) {
        Log.e("db-error", e.toString());
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387540&siteId=291194637