从零开始学android<数据存储(2)Internal Storage内部存储.三十六.>
来源:互联网 发布:linux跟踪路由命令 编辑:程序博客网 时间:2024/05/16 11:15
- Call
openFileOutput()
with the name of the file and the operating mode. This returns aFileOutputStream
.通过openFileOutput()建立FileoutputStream对象
- Write to the file with
write()
.创建Write对象并进行数据读写操作 - Close the stream with
close()
.最后关闭链接
以上就是讲数据文件保存到内部储存的基本步骤
下面用代码进行一下讲解
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="51dp" android:layout_toRightOf="@+id/button1" android:text="读取信息" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="31dp" android:layout_marginTop="142dp" android:text="保存信息" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="47dp" android:text="保存数据到内部储存" android:textAppearance="?android:attr/textAppearanceMedium" /></RelativeLayout>
package com.example.internalstorage;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button saveData, getData;private static final String FILENAME = "flyou.txt";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);saveData=(Button)this.findViewById(R.id.button1);getData=(Button)this.findViewById(R.id.button2);saveData.setOnClickListener(new View.OnClickListener() {//保存信息@Overridepublic void onClick(View v) {// TODO Auto-generated method stubFileOutputStream out = null;try {String infoString = "风飞雪未扬";out = openFileOutput(FILENAME, Context.MODE_PRIVATE);out.write(infoString.getBytes());Toast.makeText(MainActivity.this, "数据保存成功", 2).show();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if (out!=null) {out.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}});//读取信息getData.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubFileInputStream input=null;try {input=openFileInput(FILENAME);byte[] data=new byte[1024];int len=input.read(data);input.close();//System.out.println(new String(data,0,len));Toast.makeText(MainActivity.this, new String(data,0,len), 2).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}}
打开信息
使用文件浏览器查看信息
将文件导出
用记事本打开
以上操作可以很容易的实现文件的内部存储过程
官方api当中还有关于如何存储缓存文件的说明,大家可以自行实验一下
下节预报:
External Storage外部存储
1 0
- 从零开始学android<数据存储(2)Internal Storage内部存储.三十六.>
- Android数据存储--Internal Storage(内部存储)及权限
- Android数据存储之Internal Storage内部存储
- Android数据存储-Internal Storage
- Android 外部存储external storage和内部存储internal storage
- 从零开始学android<数据存储(3)External Storage外部存储.三十七.>
- android进阶(三)数据存储之Internal Storage
- android中数据的存储方式(Internal Storage)
- Android Data Storage(数据存储)之Internal Storage
- APP开发实战99-Android的内部存储(Internal Storage)
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- android 文件存储 Internal storage External storage
- 数据存储:Internal Storage与Shared Preferences
- 【安卓】Using the Internal Storage使用内部存储
- 从零开始学android<数据存储(4)Sqlite数据库存储.三十八.>
- Android数据存储(External Storage,外部存储)
- C语言的全局变量与局部变量
- 程序框架设计—WS
- 然后才开始渲染ccnode以及它的子节点,最后在afterDraw()才会绘制到帧缓存中
- 8中基本数据类型
- [1197]约瑟夫问题 (循环链表)SDUT
- 从零开始学android<数据存储(2)Internal Storage内部存储.三十六.>
- 如何把UCos-ii_在STM32上的移植
- 隐藏造型
- 深入浅出之MFC整理-----青铜笔记
- nl80211 & cfg80211 库是如何工作的?
- RT-Thread 学习笔记(四)---用Eclipse构建编译环境
- jQuery Sizzle.find [ 源码分析 ]
- 使用v7包,actionbar的问题
- 软密封闸阀双效合一使用效果非常好