Android reads the content in the Assets folder
Android reads the content in the Assets folder
The assets folder is a directory in the android program that stores the relevant external files. Android officially provides the corresponding method to access the content in this folder, therefore, we do not need to perform Code operations such as relevant path judgment. We can directly call relevant methods to open the file and get an InputStream ); then, the byte is read and decoded as the character input stream (InputStreamReader) through the corresponding character encoding method ); then, read text from the character input stream through BufferReader and store the characters in the buffer to provide efficient reading of characters, arrays, and line segments. Finally, we can read the file content row by row.
MainActivity. java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { InputStream inputStream = getResources().getAssets().open("info.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String info = ""; while ((info = bufferedReader.readLine()) != null) { Log.i("fff", info); Toast.makeText(MainActivity.this, info, 1000).show(); } } catch (IOException e) { e.printStackTrace(); } }}