Android 文件操作心得体会
android 的文件操作说白了就是Java的文件操作的处理。所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了。好了,话不多说,开始今天的正题吧。
先从一个小项目入门吧
首先是一个布局文件,这一点比较的简单,那就直接上代码吧。
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名称" />
<EditText
android:id="@+id/et_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="file name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件内容" />
<EditText
android:id="@+id/et_filecontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="7"
android:hint="file content"
/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toSave"
android:text="Save"
/>
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="getFile"
android:text="Get"
/>
</LinearLayout>
然后是我们的主界面的Java文件了。继续上代码
package com.mark.storage;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.mark.service.FileService;
public class MainActivity extends Activity {
private EditText mEt_filename,mEt_filecontent;
private Button mBtn_save;
private void init(){
mEt_filecontent = (EditText) findViewById(R.id.et_filecontent);
mEt_filename = (EditText) findViewById(R.id.et_filename);
mBtn_save = (Button) findViewById(R.id.btn_save);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/**
* 保存数据到一个文件中
* @param view
*/
public void toSave(View view) {
String fileName = mEt_filename.getText().toString();
String fileContent = mEt_filecontent.getText().toString();
FileService service = new FileService(getApplicationContext());
boolean isSucceed = service.save(fileName, fileContent);
if(isSucceed){
Toast.makeText(getApplicationContext(), "恭喜您保存文件成功!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "对不起,您保存文件失败!", Toast.LENGTH_SHORT).show();
}
}
public void getFile(View view){
String fileName = mEt_filename.getText().toString();
FileService service = new FileService(getApplicationContext());
String fileContent = service.getFile(fileName);
if(fileContent!=null || !fileContent.equals("")) {
mEt_filecontent.setText(fileContent);
}else{
Toast.makeText(getApplicationContext(), "对不起,读取文件失败!", Toast.LENGTH_SHORT).show();
}
}
}
是不是感觉里面的代码有点奇怪呢?FileService是什么鬼?
其实FileService就是我们的业务类,主要的功能就是帮助我们实现了对文件的保存和读取等操作。下面也贴出代码
package com.mark.service;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
//android自带的可以快速获得文件输出流的一个类,注意参数不能是路径,只能是文件名称
private Context mContext;
public FileService(Context context) {
this.mContext = context;
}
/**
* 保存文件的一个方法
* @param fileName
* @param fileContent
* @return
*/
public boolean save(String fileName, String fileContent) {
try {
//采用Context.MODE_PRIVATE模式的话,只允许本应用访问此文件,并且熟覆盖式的添加数据
FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(fileContent.getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 获得之前保存过的文件的详细的信息
* @param fileName
* @return
*/
public String getFile(String fileName) {
String fileContent = "";
try{
FileInputStream fis = mContext.openFileInput(fileName);
byte[] buf = new byte[1024];
int len;
ByteArrayOutputStream bais = new ByteArrayOutputStream();
while((len = fis.read(buf))!= -1){
bais.write(buf, 0, len);
}
byte[] data = bais.toByteArray();
fileContent = new String(data);
fis.close();
return fileContent;
}catch(Exception e){
e.printStackTrace();
return "对不起,读取文件失败!";
}
}
}
业务类的分析
现在开始进入正题咯。这个小项目的核心就在于这个业务类,原因如下:
- Context:Android自带的上下文类,方便获得file流对象
- 读文件方法中使用到了ByteArrayOutputStream类,这一点是很重要的,如果只是单纯的使用字符串来读取存储的文件的话,就会因为序列化的问题而出现不了目标数据。
- 使用了返回值来对操作的结果进行了“反馈”,方便为用户提供友好的界面和使用体验。
核心
分层的思想,不同的功能的类放置到不同的包内,这样既方便程序的调试,也方便今后的代码的维护。
Android 文件操作心得体会的更多相关文章
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
- Java和Android文件操作
File这是文件基类,抽象地代表一个文件实体,它有四个不同的构造方法: File(File dir, String name) File(String path) File(String dir ...
- Android 文件操作之openFileOutput
openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它.创建的文件保存在/data/data/<package ...
- Android文件操作
将数据写入Internal Storage: String fileName = "myfile.txt"; String str="保存数据到内部存储"; t ...
- 大过年的,不下班的,上个Android文件操作类(内部存储和sd卡均可)
package com.kkdiangame.UI.res; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...
- 转:Android文件操作总结
http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327597.html http://blog.sina.com.cn/s/blog_5a4 ...
- Android文件操作报open failed: EBUSY (Device or resource busy)
Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...
- 转 Android - 文件操作
一.资源文件的读取: 1) 从resource的raw中读取文件数据: String res = ""; try{ //得到资源中的Raw数据流 InputStream in = ...
- Android数据存储-文件操作
一.预备知识 1.Android中的MVC设计模式 MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...
随机推荐
- SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理
GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...
- 数据结构 单链表&顺序表
顺序表: 一般使用数组(C语言中的数组采用顺序存储方式.即连续地址存储)来描述. 优点:在于随机访问元素, 缺点:插入和和删除的时候,需要移动大量的元素. 链表: 优点:插入或删除元素时很方便,使用灵 ...
- @RequestBody和@ModelAttribute注解
一 .@RequestBody @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify( ...
- JAVA学习总结-多线程基础:
参考书籍:疯狂JAVA讲义 1.进程和线程; 进程是处于运行过程中的程序;并且具有一定的独立功能;进程是系统进行系统资源分配和调度的一个独立单位. 一般而言,进程包括以下三个特征: 独立性:进程是系统 ...
- pycharm和shell中的sys.path不一样
用pip安装了一个模块,在pycharm中不能调用,然后发现shell和pycharm中的sys.path不一样. 纳尼?还能不一样? 很明显左边的pycharm的sys.path中少了三个重要的路径 ...
- Python盗号原理-代码实现截屏键盘记录远程发送(不要做坏事)
这年头盗号的从出不穷,不是脱裤就是社工钓鱼.今天呢我们就以前面的几篇技术文章做铺垫,来讲一下python盗号的原理(学习用途,请勿用于违法犯罪),知己知彼方能百战不殆嘛. 脱裤我们就不多提了,无非就是 ...
- cannot open file "cxcore.lib"
运行例子程序的时候总是出现连接错误:LINK : fatal error LNK1104: cannot open file "cxcore.lib". 在VC选项里把C:\Pro ...
- 使用FFMPEG在windows平台下推rtmp流
使用FFMPEG在windows平台下推rtmp流 工作中习惯在Linux下面使用FFmpeg模拟推rtmp流,无奈家中的电脑都是windows系统,需要利用家中的带宽来测试流媒体服务器的性能.所以研 ...
- Java不走弯路教程(3.用户验证与文件内容查询)
3.用户验证与文件内容查询 在上一章中,我们完成了对指定文件内容的输出操作. 我们现在有如下格式的文件product.db id,product_name,product_detail 1,noteb ...
- PHP MySQL 创建数据表
PHP 创建 MySQL 表 一个数据表有一个唯一名称,并有行和列组成. 使用 MySQLi 和 PDO 创建 MySQL 表 CREATE TABLE 语句用于创建 MySQL 表. 我们将创建一个 ...