File内部存储

  1. FileOutPutStream FileInputStream

FileActivity 以及对应的xml文件

package com.example.test0508;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileActivity extends AppCompatActivity {    private EditText mEtName;    private Button mBtnSave,mBtnShow;    private TextView mTvContent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_file);        mEtName = findViewById(R.id.edit_file_name);        mBtnSave = findViewById(R.id.btn_file_save);        mBtnShow = findViewById(R.id.btn_file_show);        mTvContent = findViewById(R.id.tv_file_content);        mBtnSave.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                save(mEtName.getText().toString());            }        });        mBtnShow.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mTvContent.setText(read());            }        });    }    /**     * 用来存储数据     */    private void save(String content){        FileOutputStream fileOutputStream = null;        try {            fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE);            fileOutputStream.write(content.getBytes());        } catch (IOException e) {            e.printStackTrace();        }finally {            if (fileOutputStream != null) {                try {                    fileOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * 用来读取信息     */    private String read(){        FileInputStream fileInputStream = null;        try {            fileInputStream = openFileInput("test.txt");            byte[] buffer = new byte[1024];            StringBuffer stringBuffer = new StringBuffer();            int len = 0;            while ((len = fileInputStream.read(buffer))>0){                stringBuffer.append(new String(buffer,0,len));            }            return stringBuffer.toString();        } catch (IOException e) {            e.printStackTrace();        }        return "";    }}
<?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"    android:padding="15dp"    >    <EditText        android:id="@+id/edit_file_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入内容"        />    <Button        android:id="@+id/btn_file_save"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="保存"        android:layout_marginTop="10dp"        />    <Button        android:id="@+id/btn_file_show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示"        android:layout_marginTop="10dp"        />    <TextView        android:id="@+id/tv_file_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        /></LinearLayout>

Android之File内部存储和File外部存储_第1张图片


File外部存储

package com.example.test0508;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileActivity extends AppCompatActivity {    private EditText mEtName;    private Button mBtnSave,mBtnShow;    private TextView mTvContent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_file);        mEtName = findViewById(R.id.edit_file_name);        mBtnSave = findViewById(R.id.btn_file_save);        mBtnShow = findViewById(R.id.btn_file_show);        mTvContent = findViewById(R.id.tv_file_content);        mBtnSave.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                save(mEtName.getText().toString());            }        });        mBtnShow.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mTvContent.setText(read());            }        });    }    /**     * 用来存储数据     */    private void save(String content){        FileOutputStream fileOutputStream = null;        try {//内部存储     fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE);            /**             * 外部存储存储到sd卡             */            File dir = new File(Environment.getExternalStorageDirectory(),"skypan");            if (!dir.exists()){                dir.mkdirs();            }            File file = new File(dir,"test.txt");            if (!file.exists()){                file.createNewFile();            }            fileOutputStream = new FileOutputStream(file);            fileOutputStream.write(content.getBytes());        } catch (IOException e) {            e.printStackTrace();        }finally {            if (fileOutputStream != null) {                try {                    fileOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * 用来读取信息     */    private String read(){        FileInputStream fileInputStream = null;        try {//            fileInputStream = openFileInput("test.txt");            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"skypan","test.txt");            fileInputStream = new FileInputStream(file);            byte[] buffer = new byte[1024];            StringBuffer stringBuffer = new StringBuffer();            int len = 0;            while ((len = fileInputStream.read(buffer))>0){                stringBuffer.append(new String(buffer,0,len));            }            return stringBuffer.toString();        } catch (IOException e) {            e.printStackTrace();        }        return "";    }}

更多相关文章

  1. Android 定时获取系统内存信息
  2. Android 数据存储与读取:文件
  3. java android 获取手机操作系统相关信息
  4. Android 串口数据处理
  5. Android 关于 如何使用外界导入的数据库文件
  6. Android获取应用的签名信息
  7. Android获取WiFi ip相关信息

随机推荐

  1. 用python解析word文件(二):table
  2. python selenium-webdriver 定位frame中
  3. 恺撒密码 I Python实现
  4. Python学习之——Tcp/ip基础/IP地址/DNS/
  5. Python实现二叉树的左中右序遍历
  6. 检查 NaN 数据值 (C/C++/Python 实现)
  7. Python之高级特性
  8. 用Python学《微积分B》(有理式与简单无理
  9. python中的类与方法
  10. 在两台服务器上有效地在两个Django应用程