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

课程demo


 public class MainActivity extends AppCompatActivity {
private EditText edit;
private TextView tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit= (EditText) findViewById(R.id.edit);
tx= (TextView) findViewById(R.id.tx);
}
@Override
protected void onDestroy() {
super.onDestroy();
String input=edit.getText().toString();
save(input);
}
//保存数据
思想:通过openFileOutput方法,得到一个FileOutputStream对象,将其包裹在
OutputStreamWriter上,也生成一个对象,继续包裹在BufferedWriter上,得到一个对象
调用write()方法,把数据传入

  private void save(String input) {
FileOutputStream fileout=null;
BufferedWriter writer=null;
try {
//data:文件名 Context.MODE_PRIVATE:访问权限
fileout=openFileOutput("data", Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(fileout));
writer.write(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(writer!=null)
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void doclick(View v) {
switch (v.getId()) {
case R.id.bt:
String text = load();
tx.setText(text);
}
}
//取出数据
public String load()
{
FileInputStream input=null;
BufferedReader read=null;
//StringBuilder的对象的append方法可以将字符连接起来
StringBuilder content=new StringBuilder();
try {
input=openFileInput("data");
read=new BufferedReader(new InputStreamReader(input));
String line="";
while((line=read.readLine())!=null){
content.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
}

												

File文件存储的更多相关文章

  1. Android File文件存储功能

    1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...

  2. Android开发--数据存储之File文件存储

    转载来自:http://blog.csdn.net/ahuier/article/details/10364757,并进行扩充 引言:Android开发中的数据存储方式 Android提供了5种方式存 ...

  3. wex5 file文件存储

    在js中需要引入file的cordova包 require("cordova!cordova-plugin-file"); 如果要存到手机的根目录下,在Native文件夹的对应项目 ...

  4. (转)FastDFS文件存储

    一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...

  5. Android中使用File文件进行数据存储

    Android中使用File文件进行数据存储 上一篇学到使用SharedPerences进行数据存储,接下来学习一下使用File进行存储 我们有时候可以将数据直接以文件的形式保存在设备中, 例如:文本 ...

  6. File存储 - 文件存储

    博客地址 http://www.cnblogs.com/mmyblogs/p/6107472.html(转载请保留) 文件存储 文件存储是 Android 中最基本的一种数据存储方式,它不对存储的内容 ...

  7. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

  8. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  9. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

随机推荐

  1. github的提交源码到服务器

    github是现代的代码库,各种牛人,各种开源,也是现在大公司招聘的一个考察点, 这里介绍一下怎样把本地源码提交到github上. 首先我们需要在github上创建一个respository. 2,输 ...

  2. Android-shareSDK

    1.当数据: 地址:http://sharesdk.mob.com/Download 2.集成数据: DOS命令: java -jar QuickIntegrater.jar    (输入自己的项目名 ...

  3. Form content types

    Forms in HTML documents https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 17.13.4 Form con ...

  4. ios app 上架AppStore

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  5. Atom vim mode

    /******************************************************************** * Atom vim mode * 说明: * 想找一个具有 ...

  6. I.MX6Q MfgTool2 ucl2.xml eMMC

    /**************************************************************************** * I.MX6Q MfgTool2 ucl2 ...

  7. Consistent Hashing算法

    前几天看了一下Memcached,看到Memcached的分布式算法时,知道了一种Consistent Hashing的哈希算法,上网搜了一下,大致了解了一下这个算法,做下记录. 数据均衡分布技术在分 ...

  8. python from import与import as 的含义

    from os import makedirs, unlink, sep #从os包中引入 makedirs.unlink,sep类 from os.path import dirname, exis ...

  9. RelativeLayout和layout_weight的异曲同工之妙(转载)

    转自:http://ericbaner.iteye.com/blog/1161751 Android应用UI开发,对以上布局,可以使用RelativeLayout, 即: Xml代码 <Rela ...

  10. The Power of Android Action Bars(转载)

    转自:http://www.informit.com/articles/article.aspx?p=1743642