为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

Android 数据储存与读取文件

2018-11-13 3页 doc 15KB 39阅读

用户头像

is_447713

暂无简介

举报
Android 数据储存与读取文件Android 数据储存与读取文件 Android 数据存储与读取:文件 public class MainActivity extends Activity implements View.OnClickListener{ Button btnSave; Button btnRead; EditText edFileName; EditText edFileContent; String fileName; String fileContent; @Override public void onCreat...
Android 数据储存与读取文件
Android 数据储存与读取文件 Android 数据存储与读取:文件 public class MainActivity extends Activity implements View.OnClickListener{ Button btnSave; Button btnRead; EditText edFileName; EditText edFileContent; String fileName; String fileContent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取页面中的组件 btnSave=(Button) findViewById(R.id.btnSave); btnRead=(Button) findViewById(R.id.btnRead); //添加按钮监听 btnSave.setOnClickListener(this); btnRead.setOnClickListener(this); } public void onClick(View v) { edFileName=(EditText) findViewById(R.id.edFileName); edFileContent=(EditText) findViewById(R.id.edContent); fileName=edFileName.getText().toString(); fileContent=edFileContent.getText().toString(); switch (v.getId()) { case R.id.btnSave: save(fileName,fileContent); Toast.makeText(getApplicationContext(), 保存成功~ , 1).show(); break; case R.id.btnRead: edFileContent.setText(read(fileName)); break; } } public void save(String fileName, String fileContent) { try { //创建输出流,模式为私有模式,只能被本应用访问, FileOutputStream outStream = getApplicationContext().openFileOutput(fileName, MODE_PRIVATE); //默认会保存到 /data/data/package name/files下,如果不存在则会创建, outStream.write(fileContent.getBytes()); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } public String read(String fileName) { FileInputStream inputStream; try { inputStream = getApplicationContext().openFileInput(fileName); ByteArrayOutputStream outStream =new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int len=0; while((len=inputStream.read(buffer))!=-1){ outStream.write(buffer, 0, len); } byte[] data=outStream.toByteArray(); inputStream.close(); outStream.close(); return new String(data); } catch (Exception e) { e.printStackTrace(); } return null; } }
/
本文档为【Android 数据储存与读取文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索