Android 读取和保存文件(手机内置存储器)
1:activity_main.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">
<TextView
android:id="@+id/tv_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名称"/>
<EditText
android:id="@+id/et_filename"
android:layout_below="@id/tv_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:id="@+id/tv_content"
android:layout_below="@id/et_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件内容"/>
<EditText
android:id="@+id/et_content"
android:layout_below="@id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <Button
android:id="@+id/btn_save"
android:layout_below="@id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/> <Button
android:id="@+id/btn_read"
android:layout_below="@id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取"/>
</RelativeLayout>
2:文件操作类:FileService.java
public class FileService {
private Context context=null;
public FileService(Context context){
this.context=context;
}
//save file
public void saveFile(String filename,String content) throws Exception{
FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(content.getBytes());
out.close();
}
//read file
public String readFile(String filename)throws Exception{
FileInputStream is=context.openFileInput(filename);
byte b[]=new byte[1024];
int len=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//先把数据写入内存
while((len=is.read(b))!=-1){
baos.write(b,0,len);
}
//从内存中读取数据
byte data[]=baos.toByteArray();
baos.close();
is.close();
return new String(data);
}
}
3:MainActivity.java
public class MainActivity extends Activity {
private FileService fileService=null;
private Button btnSave=null,btnRead=null;
private EditText etFilename=null;
private EditText etContent=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);
etFilename=(EditText)findViewById(R.id.et_filename);
etContent=(EditText)findViewById(R.id.et_content);
fileService=new FileService(MainActivity.this);
btnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String filename=etFilename.getText().toString();
String content=etContent.getText().toString();
try {
fileService.saveFile(filename, content);
Toast.makeText(MainActivity.this, "Save file success!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Save file error!", Toast.LENGTH_SHORT).show();
}
}
});
btnRead.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String filename=etFilename.getText().toString();
try {
String text=fileService.readFile(filename);
etContent.setText(text);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Read file error!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
4:运行结果

Android 读取和保存文件(手机内置存储器)的更多相关文章
- JavaScript进阶(六)用JavaScript读取和保存文件
用JavaScript读取和保存文件 因为Google还不提供同步插件数据的功能,所以导入和导出插件配置就必须和文件打交道了.而出于安全原因,只有IE才提供访问文件的API:但随着HTML 5的到来, ...
- Android学习笔记——保存文件(Saving Files)
本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/J ...
- android 读取SD卡文件
SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件.因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android ...
- android 读取用户号码,手机串号,SIM卡序列号
简介: IMSI:international mobiles subscriber identity国际移动用户号码标识,这个一般大家是不知道,GSM必须写在卡内相关文件中:MSISDN:mobile ...
- [Android] Android读取Asset下文件的最简单的方法总结(用于MediaPlayer中)
方法一:getAssets().openFd //读取asset内容 private void openAssetMusic(String index) throws IOException { ...
- android 读取本地json文件 解决显示乱码显示
1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context, String fileName){ ...
- C# FileStream分块读取和保存文件
一 FileStream分块读取文件 public byte[] GetFileData(string fileName, long startPosition, long length) { byt ...
- Android学习笔记-保存数据的实现方法1
Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考. 第一种,保存数据到内存中: //java开发中的保存数据的方式 pub ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
随机推荐
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- DHTML【6】--CSS
从今天开始,我们迎来了一个新的面孔---CSS,二者这也是一个漂亮的面孔,为什么说这是一个漂亮的面孔呢?因为CSS是做特效的,可以美化HTML页面,我们看到淘宝网.网易首页等网站都非常好看,那都是一些 ...
- Android Studio中解决Gradle DSL method not found: 'android()'
近期导入as的项目出了这种问题 这个问题困扰了我非常长时间,好吧,搜了半天全都是runProguard的.最后在stackoverflow上搜到解决的方法了: http://stackoverflow ...
- 招一位安防软件project师,嵌入式开发project师
岗位职责 1.负责海思平台IPC产品应用层软件设计及维护 2.私有平台协议对接及为第三方提供技术支持. 任职资格: 1.较强的学习.领悟能力,能够高速熟悉公司现有代码. 2.熟练掌握C.C++开发语言 ...
- [转] shell文本字符串处理
第一种方法:#%*,#即截取变量前的字符(左向右截取),%表示截取后面字符(右向左截取),*匹配符 var=foodforthought.jpg ${varible##*string} 从左向右截取最 ...
- [转] Java内部类之闭包(closure)与回调(callback)
闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它 不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥 ...
- mac下的home键、end键以及insert键的替代
最近用android模拟器模拟东西,发现模拟器的home快捷键是键盘上的home键,这让我在windows下很好找,换到mac下找了老半天也没找到,后来才查到是有替代键的,放到这里做备份 home键f ...
- 【转】关于Ubuntu的sources.list 的总结
一.作用 文件/etc/apt/sources.list是一个普通可编辑的文本文件,保存了ubuntu软件更新的源服务器的地址.和sources.list功能一样的是/etc/apt/sources. ...
- 利用PCA来简化数据
13.2.2 在NUmpy中实现PCA 将数据转换成前N个主成分的伪代码大致如下: 去除平均值 计算协方差矩阵 计算协方差矩阵的特征值和特征向量 将特征值从大到小排列 保留最上面的N个特征向量 将数据 ...
- centos redis安装
对redis不是很了解,先写一个简单的安装过程 系统版本:centos 6.5 redis版本:2.8.23 一.安装依赖 sudo yum install tcl gcc gcc-c++ -y 二. ...