Android reads the content in the Assets folder

Source: Internet
Author: User

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();        }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.