How Android reads and writes assets, RAW, Sdard, and engineering files

Source: Internet
Author: User

Android development is inseparable from the operation of the file, the previous article "Android Easy data storage Sharedpreferences" and "the operation of the Android data store SQLite", respectively, explained the simple data storage and database data operation. However, the previous two articles did not cover the operation of the specific file. Let's see how Android operates its built-in catalog files and external SDcard data.

Environment: Android Studio, Hippocampus play simulator

Resource file:

/assets directory under Test.txt

aaaaaaaaaaaaaabbbbbbb

/res/raw (needs to be created manually) under directory Rawfile.txt

Raw FILEDDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF

One, read the file in the assets directory

 //  Read and write assets directory files  INP            Utstream is = Getresources (). Getassets (). Open ("test.txt" );            Reader in  = new   InputStreamReader (is); BufferedReader BufferedReader  = new   = null   while  (null ! = (line = Bufferedreader.readline ()) {System.out.println ( "Assets file==========" + line);            } bufferedreader.close ();            In.close (); Is.close ();  

Because files under assets no longer generate the corresponding IDs in R.java, Android provides a assetmanager way to access resources under assets.

Second, read the file under the raw directory

            //Read Raw directory files, raw in the Res directory, you need to manually create            is = getresources (). Openrawresource (R.raw.rawfile);             New InputStreamReader (IS);             New BufferedReader (in);             NULL ;              while (null ! = (line = bufferedreader.readline ())) {                System.out.println ("rawfile==========" + Line );            }            Bufferedreader.close ();            In.close ();            Is.close ();

Iii. Reading and writing works internal documents

           //Read and write project internal documentsFileOutputStream FileOutputStream = openfileoutput ("Projectfile.txt", context.mode_private); OutputStreamWriter OSW=NewOutputStreamWriter (FileOutputStream); BufferedWriter BW=NewBufferedWriter (OSW); Bw.write ("Ooooooooooooooooooo");            Bw.close ();            Osw.close ();            Fileoutputstream.close (); FileInputStream FileInputStream= Openfileinput ("Projectfile.txt"); InputStreamReader ISR=NewInputStreamReader (FileInputStream); Char[] input =New Char[Fileinputstream.available ()];            Isr.read (input);            Isr.close ();            Fileinputstream.close (); String Str=NewString (input); System.out.println ("Project file=============" + str);

Iv. reading and writing sdcard files

1. Add permission control in Androidmanifest.xml

<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"></ uses-permission >

2. Read and write SDcard files

            //Read and write sdcard, need to add permission description in Androidmanifest.xml//<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> File File = Environment.getexternalstoragedirectory ();//Get SDcard Path            if(File.exists ()) {//determine if there is sdcardSystem.out.println ("SDcard file path========" +File.getabsolutepath ()); File myfile=NewFile (file, "Myfile_sdcard.txt"));                Myfile.createnewfile (); FileOutputStream Fos=NewFileOutputStream (myfile); Fos.write ("Aaaaaaaaaaa". GetBytes ());                Fos.flush ();            Fos.close (); }            //Read SDcard fileFile file1 =NewFile (file, "Myfile_sdcard.txt")); InputStream InputStream=NewFileInputStream (FILE1); Reader Reader=NewInputStreamReader (InputStream); BufferedReader BufferedReader1=NewBufferedReader (reader); String iline=NULL;  while(NULL! = (Iline =Bufferedreader1.readline ())) {System.out.println ("From SDcard myfile_sdcard.txt============" +iline);            } bufferedreader1.close ();            Reader.close (); Inputstream.close ();

Run the app into the simulator, and you can see the following output

09-13 09:44:57.520 2243-2243/com.example.ibm.dataoperate i/system.out:assets file==========aaaaaaaaaaaaaa09-13 09:44:57.524 2243-2243/com.example.ibm.dataoperate i/system.out:assets file==========bbbbbbb09-13 09:44:57.528 2243-2243/com.example.ibm.dataoperate i/system.out:rawfile==========Raw File09-13 09:44:57.528 2243-2243/com.example.ibm.dataoperate i/system.out:rawfile==========ddddddddddd09-13 09:44:57.532 2243-2243/com.example.ibm.dataoperate i/system.out:rawfile==========eeeeeeeeee09-13 09:44:57.532 2243-2243/com.example.ibm.dataoperate i/system.out:rawfile==========ffffffffff09-13 09:44:57.544 2243-2243/com.example.ibm.dataoperate I/system.out:project file=============ooooooooooooooooooo09-13 09:44:57.552 2243-2243/com.example.ibm.dataoperate i/system.out:sdcard file path========/mnt/SDcard09-13 09:44:57.556 2243-2243/com.example.ibm.dataoperate i/system.out:from sdcard myfile_sdcard.txt============ Aaaaaaaaaaa

Each of the four ways to manipulate Android files varies from one process to the other.

1. Read the resource files in the assets directory by Getresources (). Getassets () method to obtain the Assetmanager open method for processing;

2, while reading the raw directory resource files through the getresources (). Openrawresource (R.raw. Resource name) way to obtain;

3, read and write the other directory of the document through Openfileoutput and Openfileinput to deal with;

4, the operation of the last SDcard file requires additional permission in the Androidmanifest.xml declaration, and through Environment.getexternalstoragedirectory () to obtain the path of SDcard, The rest is the Java operation of the file.

How Android reads and writes assets, RAW, Sdard, and engineering files

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.