zoukankan      html  css  js  c++  java
  • 安卓笔记20170117

    安卓中的文件存储

    Context类中的openFileOutput方法,可用于将数据存储到指定的文件中。此方法接受两个参数,第一个参数是文件名,在文件创建的时候使用的就是这个名称。这里指定的文件名不可以包含路径。因为所有的文件都是默认存储到/data/data/<package name>/files/目录下。

    第二个参数是文件的操作模式,主要有两种模式可选:

         MODE_PRIVATE:默认的操作模式,表示当指定同样文件名的时候,所写入的内容将会覆盖原文件中的内容。

         MODE_APPEND:表示如果文件已存在,就往文件里面追加内容,不存在就创建新文件。

    openFileOutput方法返回的是一个FileOutputStream对象。

    Context类中还提供了一个openFileInput方法,用于从文件中读取数据。只接收一个参数,即要读取的文件名,然后系统会自动到/data/data/<package name>/files/目录下去加载这个文件,并返回一个FileInputStream对象。

    下面是个简单的例子,打开app,在EditText里输入数据,关闭app时在onDestory方法里保存数据,再次打开app时,读取数据显示在EditText里面

    public class MainActivity extends Activity {
    
        private EditText edit;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            edit = (EditText) findViewById(R.id.edit);
            String inputText = load();
            if(!TextUtils.isEmpty(inputText)){
                edit.setText(inputText);
                edit.setSelection(inputText.length());
                Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show();
            }
        }
        
        @Override
        protected void onDestroy() {
            super.onDestroy();
            String inputText = edit.getText().toString();
            save(inputText);
        }
        
        public void save(String inputText){
            FileOutputStream out = null;
            BufferedWriter writer = null;
            
            try {
                out = openFileOutput("data", Context.MODE_PRIVATE);
                writer = new BufferedWriter(new OutputStreamWriter(out));
                writer.write(inputText);
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if(writer != null){
                        writer.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public String load(){
            FileInputStream in = null;
            BufferedReader reader = null;
            StringBuilder content = new StringBuilder();
            
            try {
                in = openFileInput("data");
                reader = new BufferedReader(new InputStreamReader(in));
                String line = "";
                while((line = reader.readLine()) != null){
                    content.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(reader != null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return content.toString();
        }
    }

    TextUtils.isEmpty()方法可以一次性进行两种空值的判断。当传入的字符串等于null或者等于空字符串的时候,这个方法都会返回true,从而使得我们不需要先单独判断这两种空值再使用逻辑运算符连接起来了。

  • 相关阅读:
    crm-ssh-列表显示(顾客列表,用户,联系人列表)
    leetcode- Rotate Array 旋转数组
    ssh的整合
    svn详解和使用
    leetcode-Plus One 加一
    spring-jdbc-aop事务
    leetcode-Remove Duplicates from Sorted Array
    0020 DRF框架开发(07 基类视图 GenericAPIView)
    0019 DRF框架开发(06 基类视图 APIView)
    0018 DRF框架开发(05 序列化器的字段与选项)
  • 原文地址:https://www.cnblogs.com/919czzl/p/6293909.html
Copyright © 2011-2022 走看看