android开发基础教程—文件存储功能实现

所属分类: 软件编程 / Android 阅读数: 1404
收藏 0 赞 0 分享
文件存储:
复制代码 代码如下:

public class MainActivity extends Activity {
EditText mname, mage;
TextView mtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mname = (EditText) findViewById(R.id.editText1);
mage = (EditText) findViewById(R.id.editText2);
mtv = (TextView) findViewById(R.id.textView1);
}
public void onClick(View v) {
String name = mname.getText().toString();
int age = Integer.parseInt(mage.getText().toString());
String cont = "name=" + name + ",age=" + age + "\n";
try {
int id = v.getId();
// 内部保存
if (id == R.id.button1) {
FileOutputStream fos = this.openFileOutput("mytext.txt",
Context.MODE_APPEND | Context.MODE_WORLD_WRITEABLE
| Context.MODE_WORLD_READABLE);
fos.write(cont.getBytes());
fos.close();
Toast.makeText(this, "写入完成", 1).show();
}
// 读取
else if (id == R.id.button2) {
FileInputStream fis = this.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
String str = new String(bytes);
mtv.setText(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}

其他app如果想要访问这个mytext.txt文件格式如下:
复制代码 代码如下:

public class MainActivity extends Activity {
TextView mcontent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontent=(TextView) findViewById(R.id.textView1);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.button1:
try {
readRemoteFileByAbslutePath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.button2:
try {
WriteRemoteFileByAbslutePath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
/**
* 通过文件绝对路径读取远程文件
* @throws Exception
*/
public void readRemoteFileByAbslutePath() throws Exception{
String path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
FileInputStream fis = new FileInputStream(path);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
String str = new String(bytes);
mcontent.setText(str);
Log.i("Other", str);
}
/**
* 通过文件绝对路径读取远程文件
* @throws Exception
*/
public void WriteRemoteFileByAbslutePath() throws Exception{
String path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
FileOutputStream fos = new FileOutputStream(path,true);
fos.write("other write! ".getBytes());
fos.close();
Log.i("Other", "other write over!");
}
/**
* 通过包相关上下文写入远程文件
* @throws Exception
*/
public void readRomoteByPackageContext() throws Exception {
String pname = "com.nanguabing.filedemo";
Context ctx = this.createPackageContext(pname,
Context.CONTEXT_IGNORE_SECURITY);
FileInputStream fis = ctx.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
Log.i("Other",new String(bytes));
}
/**
* 通过包相关上下文写入远程文件
*/
public void readRomoteByPackageContext2() throws Exception {
String pname = "com.nanguabing.filedemo";
Context ctx = this.createPackageContext(pname,
Context.CONTEXT_INCLUDE_CODE);
FileInputStream fis = ctx.openFileInput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
Log.i("Other",new String(bytes));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
更多精彩内容其他人还在看

Android 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多