zoukankan      html  css  js  c++  java
  • File文件存储

    文件存储的核心是Context提供了一个openFileOutput()与openFileInput()俩个方法

    课程demo

    
    
     1 public class MainActivity extends AppCompatActivity {
     2 private EditText edit;
     3     private TextView tx;
     4     @Override
     5     protected void onCreate(Bundle savedInstanceState) {
     6         super.onCreate(savedInstanceState);
     7         setContentView(R.layout.activity_main);
     8         edit= (EditText) findViewById(R.id.edit);
     9         tx= (TextView) findViewById(R.id.tx);
    10     }
    11     @Override
    12     protected void onDestroy() {
    13         super.onDestroy();
    14      String input=edit.getText().toString();
    15         save(input);
    16     }
    //保存数据
    思想:通过openFileOutput方法,得到一个FileOutputStream对象,将其包裹在

    OutputStreamWriter上,也生成一个对象,继续包裹在BufferedWriter上,得到一个对象
    调用write()方法,把数据传入
    
    
     1  private void save(String input) {
     2         FileOutputStream fileout=null;
     3         BufferedWriter writer=null;
     4         try {
     5    //data:文件名 Context.MODE_PRIVATE:访问权限
     6             fileout=openFileOutput("data", Context.MODE_PRIVATE);
     7             writer=new BufferedWriter(new OutputStreamWriter(fileout));
     8             writer.write(input);
     9         } catch (FileNotFoundException e) {
    10             e.printStackTrace();
    11         } catch (IOException e) {
    12             e.printStackTrace();
    13         }
    14         finally {
    15             if(writer!=null)
    16                 try {
    17                     writer.close();
    18                 } catch (IOException e) {
    19                     e.printStackTrace();
    20                 }
    21         }
    22     }
    23     public void doclick(View v) {
    24         switch (v.getId()) {
    25             case R.id.bt:
    26             String text = load();
    27             tx.setText(text);
    28         }
    29     }
    30 //取出数据
    31     public String load()
    32     {
    33         FileInputStream input=null;
    34         BufferedReader read=null;
    35 //StringBuilder的对象的append方法可以将字符连接起来
    36         StringBuilder content=new StringBuilder();
    37         try {
    38             input=openFileInput("data");
    39             read=new BufferedReader(new InputStreamReader(input));
    40             String line="";
    41             while((line=read.readLine())!=null){
    42                 content.append(line);
    43             }
    44         } catch (FileNotFoundException e) {
    45             e.printStackTrace();
    46         } catch (IOException e) {
    47             e.printStackTrace();
    48         }
    49         return content.toString();
    50     }
    51 }
    
    
    
    
    
  • 相关阅读:
    梅花雨控件使用时注意的...
    利用XML实现通用WEB报表打印(实现篇)
    Improve performance using ADO.NET 2.0 batch update feature
    hook
    owc11生成饼状图
    PHP数组合并:[“+”运算符]、[array_merge]、[array_merge_recursive]区别
    PHP中使用函数array_merge()合并数组
    WCF 第四章 绑定
    WCF 第四章 绑定 跨机器通信
    WCF 第六章 序列化与编码 系列文章
  • 原文地址:https://www.cnblogs.com/cct1314520/p/6485372.html
Copyright © 2011-2022 走看看