Detailed use internal storage

Highly recommended article: Welcome Favorite
Android Dry Share

Reading five minutes, ten o'clock daily, lifelong learning with you, here is the Android programmer

This article describes the Androidpart of the development of knowledge by reading this article, you will reap the following:

  1. Save the path of internal storage
  2. Internal storage (storage, reading) using the method
  3. Demo internal storage
  4. Other methods of internal storage

AndroidYou can save files directly to the internal storage, the files stored in the internal memory is a private file applications, other applications can not access when appbeing unloaded when the file will be deleted. Internal storage is often saved as a file.

1. Save the internal storage path

Saving internal storage path:
/data/data/com.**包名/files/文件名

2. The internal storage (storage, reading) using the method

1. Save the file

Common method of saving files as follows:

  • 1. Use a file name and mode of operation calls openFileOutput().
    This returns a FileOutputStream.
  • 2. Use write()written to the file.
  • 3. close()Close streaming.
    // 1.保存文件方法
    private void SaveFile() {
        String filename = etname.getText().toString().trim();
        String filecontent = etcontent.getText().toString().trim();
        if (TextUtils.isEmpty(filename) || TextUtils.isEmpty(filecontent)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        // 打开一个用来读写的文件,该文件与当前上下文所在的包相关,调用该方法不需要添加任何全选,
        // 保存在手机内部存储中
        try {
            // 打开输出流,并创建文件
            FileOutputStream fos = openFileOutput(filename, MODE_PRIVATE);
            // 输入的内容保存到文件中
            fos.write(filecontent.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG)
                .show();
        etname.setText("");
        etcontent.setText("");

    }

2. Read File content method

  1. Call openFileInput()and pass the file name to be read. This returns a FileInputStream.
  2. Use read()read the file byte.
  3. Then close()close the stream.
    // 2.打开文件方法
    private void OpenFile() {
        String filename = etname.getText().toString().trim();
        if (TextUtils.isEmpty(filename)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        try {
            // 打开文件得到一个只读的输入流,
            FileInputStream fis = openFileInput(filename);
            // 将文件内容存放的byte数组中
            byte[] buffer = new byte[fis.available()];
            // 读取数组中的内容
            fis.read(buffer);
            fis.close();
            // 将数组内容存放到字符串中,并显示出来
            etcontent.setText(new String(buffer));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

3. Delete the file method

To delete a file, you can delete the file name deleteFile(filename);

    // 3.删除文件方法
    private void DeleteFile() {
        String filename = etname.getText().toString().trim();
        if (TextUtils.isEmpty(filename)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        // 删除上下文中指定名称的文件,
        boolean deletefile = deleteFile(filename);
        if (deletefile) {
            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG)
                    .show();

        }
    }

Case 3. The internal storage

  • Achieve results is as follows:
    to achieve save the file, read, delete function

Internal storage usage

  • Codes are as follows:
public class FileInternalStorageMethods extends Activity implements
        OnClickListener {
    /*
     * 内部存储的方法FileOutputStream(filename, MODE_PRIVATE);
     * FileInputStream(filename); deleteFile(filename); 文件保存位置:
     * /data/data/包名/files/文件名 内部存储的特点: 内部存储里的东西会随着app的卸载而清掉
     * 
     * *
     */

    private EditText etname, etcontent;
    private Button savebtn, openbtn, deletebtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internal_methods);
        InitView();
    }

    private void InitView() {
        etname = (EditText) findViewById(R.id.editfilename);
        etcontent = (EditText) findViewById(R.id.edit_filecontext);
        savebtn = (Button) findViewById(R.id.savefile);
        openbtn = (Button) findViewById(R.id.openfile);
        deletebtn = (Button) findViewById(R.id.deletefile);

        savebtn.setOnClickListener(this);
        openbtn.setOnClickListener(this);
        deletebtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.savefile:

            SaveFile();
            break;
        case R.id.openfile:

            OpenFile();
            break;
        case R.id.deletefile:

            DeleteFile();
            break;
        default:
            break;
        }

    }

    // 1.保存文件方法
    private void SaveFile() {
        String filename = etname.getText().toString().trim();
        String filecontent = etcontent.getText().toString().trim();
        if (TextUtils.isEmpty(filename) || TextUtils.isEmpty(filecontent)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        // 打开一个用来读写的文件,该文件与当前上下文所在的包相关,调用该方法不需要添加任何全选,
        // 保存在手机内部存储中
        try {
            // 打开输出流,并创建文件
            FileOutputStream fos = openFileOutput(filename, MODE_PRIVATE);
            // 输入的内容保存到文件中
            fos.write(filecontent.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG)
                .show();
        etname.setText("");
        etcontent.setText("");

    }

    // 2.打开文件方法
    private void OpenFile() {
        String filename = etname.getText().toString().trim();
        if (TextUtils.isEmpty(filename)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        try {
            // 打开文件得到一个只读的输入流,
            FileInputStream fis = openFileInput(filename);
            // 将文件内容存放的byte数组中
            byte[] buffer = new byte[fis.available()];
            // 读取数组中的内容
            fis.read(buffer);
            fis.close();
            // 将数组内容存放到字符串中,并显示出来
            etcontent.setText(new String(buffer));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 3.删除文件方法
    private void DeleteFile() {
        String filename = etname.getText().toString().trim();
        if (TextUtils.isEmpty(filename)) {
            Toast.makeText(getApplicationContext(), "文件名不能为空",
                    Toast.LENGTH_LONG).show();
            return;
        }
        // 删除上下文中指定名称的文件,
        boolean deletefile = deleteFile(filename);
        if (deletefile) {
            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG)
                    .show();

        }
    }

}
  • Implement the layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editfilename"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:background="@android:drawable/edit_text"
            android:hint="请输入文件名"
            android:padding="5dp"
            android:textSize="20sp" />

        <Button
            android:id="@+id/savefile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            android:text="保存"
            android:textSize="25sp" />

        <Button
            android:id="@+id/openfile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            android:text="打开"
            android:textSize="25sp" />
    </LinearLayout>

    <Button
        android:id="@+id/deletefile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:text="删除文件"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/edit_filecontext"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:drawable/edit_text"
        android:gravity="left"
        android:hint="请输入文件内容"
        android:padding="5dp"
        android:textSize="20sp" />

</LinearLayout>

4. Other methods of internal storage

1. Get the absolute path of the directory where the file system is stored inside the file.

 getFilesDir()

2. Create within your internal storage space (or open an existing) directory.

 getDir()

3. Return your application to save the current set of files.

 fileList()

So far herein, this has ended, if the wrong place, welcome your suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Micro-channel public concern number: Programmer Android, receive welfare

Guess you like

Origin www.cnblogs.com/wangjie1990/p/11310904.html
Recommended