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= ...
随机推荐
- [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记
1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...
- KMP学习
刚才看了(连接)写的翻译,把kmp算法的工作过程讲的很不错,kmp算法的核心是next,next为什么要那么赋值?其实就是前缀和后缀的最大匹配值,用这个值在匹配失败的时候可以跳过一个不必要的匹配. ...
- C - A Plug for UNIX - poj 1087(最大流)
题目大意:这个题意有些蛋疼,看了很大会才明白什么意思,有N个插座,这些插座都是有类型的只能给这种类型的电器充电,下面接着给了M种电器,和电器的插头类型,还有K种转换器,可以把一种类型转换成另一种,转换 ...
- config large memory
C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...
- js打开新的链接下载文件
var p =params.join("&"); var a = document.createElement('a'); a.href = 'report/exportp ...
- curl_setopt函数相关应用及介绍(转)
一.要想使用curl_setopt 这个函数必须在服务器里边进行编译curl这个组件,怎么安装编译这个组件请具体到google搜索 二.curl_setopt的php帮助文档的解释 bool curl ...
- Linux下查看所有用户(shell脚本获取)
在Linux系统中,使用者账号管理最重要的两个文件是/etc/password和/etc/shadow.在/etc/password文件中,每一行都代表一个账号,但是有很多账号是系统账号.比如:b ...
- android开发之——混淆编译
众所周知,android的apk文件是非常容易被反编译的,这样对于开发者来说,辛辛苦苦开发应用被破解是一件很令人懊恼的事情,谷歌也认识到了这一点,所以从2.3之后就为开发者提供了一个代码混淆工具pro ...
- Resizing LVM Logical Volumes-lvextend
1. fdisk命令/dev/sdc再分出一个sdc2分区 [root@rhel7 ~]# fdisk /dev/sdc Welcome to fdisk (util-linux ). Changes ...
- 【转】[Algorithm]01分数规划
因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...