Android数据存储————文件存储(内部存储)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_29675423/article/details/85699131

下一篇:Android数据存储————文件存储(外部存储)https://blog.csdn.net/sinat_29675423/article/details/85705545

存储特点

将数据存储在app的目录下,内部存储的数据只能被自己的应用程序访问到,存储空间有限。

存储位置

/data/data/<package_name>/files
在这里插入图片描述

存储模式

MODE_PRIVATE : 覆盖
MODE_APPEND: 追加

方法

保存:openFileInput(filename, mode)
读取:openFileOutput(filename)

例子:存储、读取字符串

将输入框中的字符串保存到内部存储路径中,并且读取存储的字符串到文本框中显示。

布局文件

<?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"
   >
    <EditText
        android:id="@+id/etNote"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
    <Button
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btnGet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取"
        android:onClick="onClick"/>
    <TextView
        android:id="@+id/tvContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity,实现读取和存储

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEtNote = findViewById(R.id.etNote);
        mTvNote = findViewById(R.id.tvContent);
    }
    private EditText mEtNote;
    private TextView mTvNote;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSave:
                //保存编辑器里面的字符串到文件中
                String content = mEtNote.getText().toString().trim();
                Boolean success = saveToInternalFile("mynote.txt", content);
                //提示是否保存成功
                if (success) {
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnGet:
                //读取文件内容,显示在文本框中
                String res = readFromInternalFile("mynote.txt");
                mTvNote.setText(res);
        }
    }
    /**
     * 读取文件内容
     * @param filename 文件名
     * @return 返回文件中的字符串
     */
    @Nullable
    private String readFromInternalFile(String filename) {
        FileInputStream fis = null;
        BufferedReader reader = null;
        try {
            fis = openFileInput(filename);
            reader = new BufferedReader(new InputStreamReader(fis));
            StringBuilder builder = new StringBuilder();
            String line;
            if((line = reader.readLine())!=null){
                builder.append(line);
            }
            return builder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
                if(reader != null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 保存到文件
     * @param filename 文件名
     * @param content 字符串内容
     * @return 保存是否成功
     */
    private boolean saveToInternalFile(String filename, String content) {
        if(filename == null || filename.length() == 0 || content == null ||content.length() == 0){
            return false;
        }
        FileOutputStream fos = null;
        try {
            fos = openFileOutput(filename, MODE_PRIVATE);
            fos.write(content.getBytes());
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return false;
    }
}

Manifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="你的包名">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

猜你喜欢

转载自blog.csdn.net/sinat_29675423/article/details/85699131