android文件存储教程,android开发基础教程—文件存储功能实现
文件存储:
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文件存储教程,android开发基础教程—文件存储功能实现相关推荐
- android程序开发教案,Android移动应用开发基础教程教案.doc
教 案 课 程 名 称Android移动应用开发基础教程(微课版)课 程 代 码系(部)教研室(实验室)授 课 教 师职 称 年 月 课 程 名 称Android移动应用开发基础教程(微课版)总计: ...
- android界面数据存储,Android应用开发基础之数据存储和界面展现(二)
Android应用开发基础之数据存储和界面展现(二) 常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRight ...
- Android App开发基础篇—数据存储(SQLite数据库)
Android App开发基础篇-数据存储(SQLite数据库) 前言:Android中提供了对SQLite数据库的支持.开发人员可以在应用中创建和操作自己的数据库来存储数据,并对数据进行操作. 一. ...
- Xamarin图表开发基础教程(8)OxyPlot框架
Xamarin图表开发基础教程(8)OxyPlot框架 [示例OxyPlotFormsDemo]在Xamarin.Forms中实现线图的显示. (1)打开Xamarin.Forms项目. (2)将Ox ...
- Xamarin图表开发基础教程(7)OxyPlot框架
Xamarin图表开发基础教程(7)OxyPlot框架 Xamarin.Forms中使用OxyPlot框架 在Xamarin. Forms平台上实现图表显示需要完成以下的步骤: 1.添加OxyPlot ...
- Xamarin图表开发基础教程(4)OxyPlot框架
Xamarin图表开发基础教程(4)OxyPlot框架 XamaminAndroid中绘制线图OxyPlotAndroidDemo [示例1-1:OxyPlotAndroidDemo]下面实现线图的绘 ...
- Xamarin图表开发基础教程(3)OxyPlot框架
Xamarin图表开发基础教程(3)OxyPlot框架 Xamarin.Android中使用OxyPlot框架 在Xamarin.Android平台上实现图表显示需要完成以下的步骤: 1.添加OxyP ...
- Xamarin图表开发基础教程(2)OxyPlot框架
Xamarin图表开发基础教程(2)OxyPlot框架 OxyPlot图表设计 OxyPlot是一个基于.Net的跨平台图表库.该图表库也支持Xamarin应用开发.该组件支持多种类型的图表.本章将主 ...
- Xamarin图表开发基础教程(1)
Xamarin图表开发基础教程(1) 在Xamarin图表开发中,最常用的框架是OxyPlot和Microcharts.其中,OxyOPlot提供多种多样的图表类型和丰富的图表功能,可以实现各种复杂的 ...
- Unity4.x 2D游戏开发基础教程第1章Unity及其组成的介绍
Unity4.x 2D游戏开发基础教程第1章Unity及其组成的介绍 本书主要讲解的是,如何使用Unity开发2D游戏.但在开始讲解之前,最好先熟悉一下Unity这个工具.本章会首先介绍Unity的下 ...
最新文章
- 找回 : MobileCoreServices.framework
- name 'false' is not defined
- Feed43自定义 RSS 订阅源
- 基于sobel算法的边缘检测设计与实现
- Mybatis的模糊查询
- NodeJS在CentOs7下安装
- 搜索引擎核心技术与算法 —— 倒排索引初体验
- 数据分析学习笔记——Pandas库思维导图
- 【转】优秀的Java程序员必须了解GC的工作原理
- Pazera Free MP4 To MP3 Converter 1.6 中文64位+32位便携版,免费的视频转换器
- 2021-07-26 NLP词嵌入
- markdown文本居中
- C# Message类的属性Msg所关联的消息ID
- 菜鸟修行之路--Echarts数据可视化知识总结完整版
- Winform中的SaveFileDialog保存文件
- 【转载】何时使用领域驱动设计
- 系统安全及应用--账号安全控制
- 高斯-赛德尔迭代(Gauss–Seidel method)c语言实现
- 使火狐浏览器默认在新的标签页打开书签
- 文档管理系统二——文档扫描与图片编辑