• Android file内部存储


    通过file=openFileOutput()获得,将数据存储在data/data/+包名+files下面。

    代码如下:

    MainActivity.java:

    复制代码
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.example.helloworld.R;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileActivity extends AppCompatActivity {
    
        private EditText mEtName;
        private Button mBtnSave,mBtnShow;
        private TextView mTvContent;
        private final String mFileName = "test.txt";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_file);
            mEtName = findViewById(R.id.et_name);
            mBtnSave = findViewById(R.id.btn_save);
            mBtnShow = findViewById(R.id.btn_show);
            mTvContent = findViewById(R.id.tv_content);
    
            mBtnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    save(mEtName.getText().toString());
                }
            });
            mBtnShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mTvContent.setText(read());
                }
            });
        }
    
        //存储数据
        private void save(String content){
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = openFileOutput(mFileName,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(mFileName);
                byte[] buff = new byte[1024];
                //用StringBuilder来实现字符串拼接
                StringBuilder sb = new StringBuilder();
                int len = 0;
                while((len = fileInputStream.read(buff)) > 0){
                    sb.append(new String(buff,0,len));
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fileInputStream != null){
                    try{
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    }
    复制代码

    运行截图:

  • 相关阅读:
    完美串(区间dp)
    Brackets(区间dp)
    Eureka的高可用
    在Spring Boot中使用 @ConfigurationProperties 注解
    Spring Boot干货系列:(四)开发Web应用之Thymeleaf篇
    luogu3707 相关分析 (线段树)
    luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
    bzoj4504 K个串 (优先队列+主席树)
    bzoj4336 骑士的旅行 (树链剖分+multiset)
    suoi37 清点更多船只 (卡空间线段树)
  • 原文地址:https://www.cnblogs.com/hrzgj/p/14913419.html
Copyright © 2020-2023  润新知