19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下。
先改下布局界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件名称" /> <EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:id="@+id/filecontent"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件内容" /> <EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存文件" /> <Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取文件" />
</LinearLayout> </LinearLayout>
再改下MainActivity文件:
package com.example.filesave; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private Context context = this;
private Button btnsave = null;
private Button btnread = null;
private EditText editname = null;
private EditText editcontent = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnsave = (Button) findViewById(R.id.btn_save);
btnread = (Button) findViewById(R.id.btn_read);
editname = (EditText) findViewById(R.id.edit_name);
editcontent = (EditText) findViewById(R.id.edit_content); btnsave.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { String filename = editname.getText().toString();
String filecontent = editcontent.getText().toString();
FileOutputStream out = null;
try {
out = context
.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(filecontent.getBytes("UTF-8")); editname.setText("已经保存名称!");
editcontent.setText("已经保存内容!"); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}); btnread.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
String filename = editname.getText().toString(); // 获得读取的文件的名称
FileInputStream in = null;
ByteArrayOutputStream bout = null;
byte[] buf = new byte[1024];
bout = new ByteArrayOutputStream();
int length = 0;
try {
in = context.openFileInput(filename); // 获得输入流
while ((length = in.read(buf)) != -1) {
bout.write(buf, 0, length);
}
byte[] content = bout.toByteArray();
editcontent.setText(new String(content, "UTF-8")); // 设置文本框为读取的内容
} catch (Exception e) {
e.printStackTrace();
}
editcontent.invalidate(); // 刷新屏幕
try {
in.close();
bout.close();
} catch (Exception e) {
}
} }); } }
运行效果:
分别输入文件名称和文件内容为“aaaa"、”1234“,如图:
点击保存文件后,界面变为:
然后再修改文件名称为”aaaa",如图:
最后点击读取文件按钮,文件内容显示为”1234”,测试成功。如图:
提示:创建的存储文件保存在/data/data/<package name>/files文件夹下,那么我们怎样才能看到这文件呢?首先连接手机处于调试模式,进入adb shell命令行界面, 输入“su”命令回车,如图
然后再输入:cd data/data 如图:
再输入ls 命令,找到自己创建工程包名文件就可以了,比如我的工程是“com.example.filesave”,那就cd com.example.filesave即可,如图:
最后找到files就可看到文件了。
19.Android之文件存储方法学习的更多相关文章
- Android File文件存储功能
1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- Android的文件存储
//文件的写入 String content1 = edt_file.getText().toString(); //用于文件的写操作 FileOutputStream fos=null; //缓冲输 ...
- Android - 读取文件存储的数据
存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...
- Android资源文件命名规范学习手册
[推荐] 资源文件需带模块前缀.[推荐] layout 文件的命名方式. Activity 的 layout 以 module_activity 开头 Fragment 的 layout 以 modu ...
- Android数据存储之Android 6.0运行时权限下文件存储的思考
前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...
- android 开发-数据存储之文件存储
android的文件存储是通过android的文件系统对数据进行临时的保存操作,并不是持久化数据,例如网络上下载某些图片.音频.视频文件等.如缓存文件将会在清理应用缓存的时候被清除,或者是应用卸载的时 ...
随机推荐
- ehcache 一二事 - ssm 中ehcashe的简单配置应用
Ehcache是一个开源Java分布式缓存.可以配合mybatis来使用 首先,在资源文件夹中新建ehcache.xml 内容如下: <?xml version="1.0&qu ...
- uGUI练习(一) Anchor
一.练习步骤 如果用过NGUI的Anchor,我们知道在2.x的版本有UIAnchor组件(下图左),3.x版本中,每个UIWidget有自带的Anchors(下图右) 而uGUI的Anchor用起来 ...
- Flash剪贴板功能
做JS的都知道,如果不考虑浏览器的兼容问题,其实,JS本身的window.clipboardData对象是可以做到复制内容到剪贴板的功能,但除了IE浏览器,FF和Chrome浏览器都不支持.现在为了浏 ...
- DataGridView 行、列的隐藏和删除
) 行.列的隐藏 [VB.NET] ' DataGridView1的第一列隐藏 DataGridView1.Columns(0).Visible = False ' DataGridView1的第一行 ...
- SOS.dll (SOS Debugging Extension)
SOS.dll (SOS Debugging Extension) lays threads associated with a live thread. The -special option di ...
- slf4j,log4j,logback 初步使用
log4j,slf4j,logback简单介绍见 LogBack简易教程 Logback浅析 简单的将,slf4j是一个日志的框架,有各种日志的接口,但是并不包含实际的写日志的方法. log4j,lo ...
- js实现倒计时 类似团购网站
一.demo与效果展示 为节约时间,我就直接套用了企鹅团的界面作为demo的背景.因为是倒计时,所以需要一个固定的时间,为了n年后,某位仁兄打开demo页面依然在倒计时,所以我把倒计时时间设成了205 ...
- windows 7 和 Ubuntu的双系统安全删除Ubuntu
1,准备文件 百度云盘链接:http://pan.baidu.com/s/1kVxuwSn 密码:e8ma 2,操作流程 #1,进入win7,将第一步下载的文件放在C:\windows\system3 ...
- WebSocket 服务器4
Java Websocket实例 Websocket 2015-04-11 14:11:54 发布 您的评价: 4.4 收藏 6收藏 介绍 现很多网站为了实现即时通讯,所用 ...
- 关于java按位操作运算
<1>.在了解位移之前,先了解一下正数和负数的二进制表示形式以及关系:举例15和-15:15 的原码: 00000000 00000000 00000000 00001111 补码 ...