Android file operations-read the content in assets and raw files, androidassets

Source: Internet
Author: User

Android file operations-read the content in assets and raw files, androidassets

1. Create the assets folder and res/raw folder respectively. (Note that the raw file is new under res and a folder named raw is created)

2. Create two txt files and copy them to the asset and raw folders:

3. Implementation results:

4. Implementation Code:

(1) Layout file:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:orientation="vertical" 7     tools:context="base.readassetsfile.MainActivity"> 8     <Button 9         android:textSize="20sp"10         android:text="@string/aasets_txt"11         android:id="@+id/readFile"12         android:layout_width="match_parent"13         android:layout_height="wrap_content" />14     <Button15         android:textSize="20sp"16         android:text="@string/raw"17         android:id="@+id/readRawFile"18         android:layout_width="match_parent"19         android:layout_height="wrap_content" />20 </LinearLayout>
View Code

(2) specific implementation:

1 package base. readassetsfile; 2 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 import android. util. log; 6 import android. view. view; 7 import android. widget. editText; 8 9 import java. io. bufferedReader; 10 import java. io. IOException; 11 import java. io. inputStream; 12 import java. io. inputStreamReader; 13 import java. io. outputStream; 14 import java. io. unsupportedEncodingEx Ception; 15 16 public class MainActivity extends AppCompatActivity implements View. onClickListener {17 @ Override18 protected void onCreate (Bundle savedInstanceState) {19 super. onCreate (savedInstanceState); 20 setContentView (R. layout. activity_main); 21 findViewById (R. id. readFile ). setOnClickListener (this); 22 findViewById (R. id. readRawFile ). setOnClickListener (this); 23} 24 @ Override25 public void onClick (View v) {26 switch (v. getId () {27 case R. id. readFile: 28 readAsset (); 29 break; 30 case R. id. readRawFile: 31 readRaw (); 32 break; 33} 34} 35 public void readAsset () {36 try {37 // get the byte 38 InputStream inputStream = getResources () in the file (). getAssets (). open ("Test.txt"); 39 // convert byte to character 40 InputStreamReader isReader = new InputStreamReader (inputStream, "UTF-8 "); 41 // use bufferReader to read content 42 BufferedReader reader = new BufferedRe Ader (isReader); 43 String out = ""; 44 while (out = reader. readLine ())! = Null) {45 Log. d ("Read File Information:", out); 46} 47} catch (IOException e) {48 e. printStackTrace (); 49} 50} 51 public void readRaw () {52 try {53 // get the content in the file 54 InputStream inputStream = getResources (). openRawResource (R. raw. test); 55 // convert the bytes in the file into characters 56 InputStreamReader isReader = new InputStreamReader (inputStream, "UTF-8 "); 57 // use bufferReader to read character 58 BufferedReader reader = new BufferedReader (isReader); 59 String out = ""; 60 t Ry {61 while (out = reader. readLine ())! = Null) {62 Log. d ("data read from raw Folder:", out); 63} 64} catch (IOException e) {65 e. printStackTrace (); 66} 67} catch (UnsupportedEncodingException e) {68 e. printStackTrace (); 69} 70} 71 72}
View Code

 

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.