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>


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(安卓)读取已知包名的uses-permission
  2. Android读取XML文件(SAX)
  3. Android连接mysql demo_Android实现登陆功能,Android与服务器数据
  4. Android(安卓)关于 如何使用外界导入的数据库文件
  5. android中Json数据保存方式
  6. Android腾讯微薄客户端开发十一:博主的粉丝
  7. Android(安卓)数据存储与读取:文件
  8. Android(安卓)串口数据处理
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. Android下实现一个Activity的全屏显示
  2. 关于build.gradle配置文件详细参数讲解
  3. Android知识体系总结(全方面覆盖Android
  4. Android(安卓)EditText OnTouchListener
  5. android notification,notificationmanage
  6. Android wifi信号强度与图标对应关系
  7. 【阿里聚安全·安全周刊】一种秘密窃取数
  8. Android IPC(一)
  9. 设置菜单默认值设置
  10. Intent的Action实现应用程序之间的切换