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的文件系统对数据进行临时的保存操作,并不是持久化数据,例如网络上下载某些图片.音频.视频文件等.如缓存文件将会在清理应用缓存的时候被清除,或者是应用卸载的时 ...
随机推荐
- Spring addFlashAttribute
redirectAttributes.addFlashAttribute("result",accountModel); 用这个可以绑定session 但是只能用一次,可以避免最后 ...
- 13Spring_AOP编程(AspectJ)_后置通知
后置通知和前置通知差不多.最大的特点是因为后置通知是运行在目标方法之后的,所以他可以拿到目标方法的运行的结果. 给出案例: 案例结构图:
- Quartz Cron 触发器 Cron Expression 的格式
转自:http://blog.csdn.net/yefengmeander/article/details/5985064 上一文中提到 Cron触发器可以接受一个表达式来指定执行JOB,下面看看这个 ...
- C语言 百炼成钢2
//题目4:输入某年某月某日,判断这一天是这一年的第几天? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<st ...
- [转]开发Visual Studio风格的用户界面--MagicLibrary使用指南
本文的示例代码为可以从这里下载: 1 概述 微软Visual Studio.NET开发工具推出已经好几年了,这个开发工具一推出就以其易用性和强大功能深受开发者的喜爱.尤其是.NET ...
- js window.open()实现打印,如何在关闭打印窗口时刷新父窗口
var childWin = window.open("your URL"); //获取子窗口句柄childWin.onunload = function(){ //onunloa ...
- MANIFEST.INF!JAR规范中
MANIFEST.INF!JAR规范中 META-INF 目录中内容心得.顺带整理了网上资料,提供地址 标签: jarjava产品sunantapache 2012-03-31 17:09 2768人 ...
- matlab 中的textscan
textread 与textscan的区别 textscan更适合读入大文件: textscan可以从文件的任何位置开始读入,而textread 只能从文件开头开始读入: textscan也可以从上 ...
- FPGA学习之基本结构
如何学习FPGA中提到第一步:学习.了解FPGA结构,FPGA到底是什么东西,芯片里面有什么,不要开始就拿个开发板照着别人的东西去编程.既然要开始学习FPGA,那么就应该从其基本结构开始.以下内容是我 ...
- Mac无法找到摄像头问题解决
facetime显示“未检测到摄像头”之类的,重启后可能摄像头有工作正常了,摄像头不稳定 重置 NVRAM后恢复正常,据说机器卡的时候,此法也可以使用. https://support.apple.c ...