Android Read and Write Files

Read and write files in Android

  • RAM: Running memory, equivalent to computer memory
  • ROM: Internal storage space, equivalent to a computer's hard disk
    • Android phones must have
  • SD card: external storage space, equivalent to the computer's mobile hard disk
    • Not required
  • Now the space that the phone has is all external storage, and then the phone basically shares the same storage device inside and outside

Internal Storage Path

  • All applications installed on the phone will generate a package name folder in the data/data directory, which is the path to the internal storage
  • Applications can only read and write files in their package name folders
  • Read and write files using path api
  • The path to the file object from getFilesDir() is data/data/package name/files
    • Files stored in this path will remain as long as you do not delete them
  • The path to the file object obtained by getCacheDir() is data/data/package name/cache

    • Files stored in this path are cached files and may be deleted when memory is low
  • Clearing the cache for the system management application interface clears everything under the cache folder, clears data, and clears everything under the entire package name directory

       public void login(View v) {
        String name = et_name.getText().toString();
        String pass = et_pass.getText().toString();

        CheckBox cb = (CheckBox) findViewById(R.id.cb);
        // Determine if the check box is checked
        if (cb.isChecked()) {
            // Determine File Name and Path
            // File file = new File("data/data/package name/info.txt");

            // Returns a File object encapsulated by data/data/package name/files
            // File file = new File(getFilesDir(), "info.txt");

            // Returns a File object encapsulated by data/data/package name/cache
            File file = new File(getCacheDir(), "info.txt");
            try {
                FileOutputStream fos = new FileOutputStream(file);
                // Write account password to local file
                fos.write((name + "&&" + pass).getBytes());
                fos.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private void readAccount() {
        // Read files, echo data
        // File file = new File("data/data/application package name/info.txt");
        // File file = new File(getFilesDir(), "info.txt");
        File file = new File(getCacheDir(), "info.txt");
        if (file.exists()) {
            try {
                FileInputStream fis = new FileInputStream(file);
                // Convert Byte Stream to Character Stream
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        fis));
                // Read text from file
                String text = br.readLine();

                String s[] = text.split("&&");

                // Set Text to Input Box
                et_name.setText(s[0]);
                et_pass.setText(s[1]);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

External Storage Path

Before 2.2: sdcard
2.2~4.2: mnt/sdcard
4.3 Beginning: storage/sdcard

Writing sd card requires permission

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

* Read the sd card without permissions until 4.0 and can be set to require after 4.0

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • Use api to get the real path of sd card, some mobile brand will change the path of sd card

    Environment.getExternalStorageDirectory()
    
  • Determine if the sd card is ready

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    
  public void login(View v){
        //Get the account password entered by the user
        String name = et_name.getText().toString();
        String pass = et_pass.getText().toString();

        CheckBox cb = (CheckBox) findViewById(R.id.cb);
        //Determine if the check box is checked
        if(cb.isChecked()){
//          File file = new File("sdcard/info.txt");

            //MEDIA_REMOVED:sd card does not exist
            //MEDIA_UNMOUNTED:sd card exists but is not mounted
            //MEDIA_CHECKING:sd card is traversing
            //MEDIA_MOUNTED:sd card available
            //MEDIA_MOUNTED_READ_ONLY:sd card available, but read-only
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                //Returns a File object encapsulating the true path of external storage
                File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    //Write account password to local file
                    fos.write((name + "&&" + pass).getBytes());
                    fos.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else{
                Toast.makeText(this, "SD Card not available for relatives(づ ̄ 3 ̄づ", 0).show();
            }
        }
    }
  private void readAccount() {
//      File file = new File("sdcard/info.txt");
        File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
        if(file.exists()){
            try {
                FileInputStream fis = new FileInputStream(file);
                //Convert Byte Stream to Character Stream
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                //Read text from file
                String text = br.readLine();

                String s[] = text.split("&&");

                //Set Text to Input Box
                et_name.setText(s[0]);
                et_pass.setText(s[1]);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

Reference resources: Android Storage

Keywords: Android Mobile

Added by rwoods on Sun, 23 Jun 2019 20:26:05 +0300