Good day. There is a method that works when the application starts:
void loadFile() throws IOException { AssetManager am = getAssets(); InputStream inputStream = am.open("text.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while ((line = reader.readLine()) != null) { if(line.equals("===")){ arr_full.add(toarr); toarr = ""; }else { toarr = toarr.concat(line).concat("\n"); } } reader.close(); } Since the file is too big (3.89 mb), launching the application takes up to 3-5 seconds. Please tell me how to execute this method in another thread so that the main UI thread runs without delay. Thank.
UPD
MyApp.java
import android.app.Application; import android.content.res.AssetManager; import android.os.AsyncTask; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class MyApp extends Application { String line; String toarr = ""; ArrayList<String> arr = new ArrayList<>(); ArrayList<String> arr_full = new ArrayList<>(); int i, j, page; LoadFile lf; void loadFile() throws IOException { AssetManager am = getAssets(); InputStream inputStream = am.open("text.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while ((line = reader.readLine()) != null) { if(line.equals("===")){ arr_full.add(toarr); toarr = ""; }else { toarr = toarr.concat(line).concat("\n"); } } reader.close(); } @Override public void onCreate() { super.onCreate(); MySingleton.initInstance(); if(MySingleton.getArr().size() == 0){ lf = new LoadFile(); lf.execute(); } } class LoadFile extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { try { loadFile(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); MySingleton.setArr(arr_full); } } } Log
08-22 20:16:34.107 518-518/com.app.app D/myLogs: запуск onPreExecute чтения файла 08-22 20:16:34.117 518-558/com.app.app D/myLogs: запуск doInBackground чтения файла 08-22 20:16:34.985 518-518/com.app.app D/myLogs: запуск onPreExexute парсера 08-22 20:16:37.862 518-518/com.app.app D/myLogs: запуск onPostExecute чтения файла 08-22 20:16:39.971 518-676/com.app.app D/myLogs: запуск doInBackground парсера 08-22 20:16:40.785 518-518/com.app.app D/myLogs: запуск onPostExecute парсера