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则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...
随机推荐
- solr6.6初探之配置篇
一.solr的简介 1) solr是企业级应用的全文检索项目,它是基于Apache Lucence搜索引擎开发出来的用于搜索的应用工程 2) solr最新版本6.6 下载地址:下载地址 二 启动与配置 ...
- PHP中利用DOM和simplxml读取xml文档
实例 用DOM获取下列xml文档中所有金庸小说的书名,该xml文档所在位置为 ./books.xml: <?xml version="1.0" encoding=" ...
- C语言程序设计第六次作业——循环结构(2)
C语言程序设计第六次作业--循环结构(2) 之前的博客园图片没处理好,对大家说一声抱歉.希望大家能够多多指出我的错误,我来认真修改 ^ - ^ !. (1)改错题 序列求和:输入一个正实数eps,计算 ...
- Oracle VM VirtualBox虚拟机内Linux系统硬盘扩容步骤(CentOS6.5)
1.首先获取要扩容的虚拟机的 UUID VBoxManage 改命令在C:\Program Files\Oracle\VirtualBox目录内,可先添加该目录到环境变量.C:\Users\yzkj- ...
- intellij配置hibernate自动生成hbm.xml文件
1.首先创建一个Java web项目,这里因为已经在整个项目中配置好tomcat了,所以我是直接创建module的,其实和创建project的配置方法一样,创建的时候选择Web Application ...
- Jackson工具
Jackson Jackson包含一个core JAR,和两个依赖core JAR的JAR: jackson-core-2.2.3.jar(核心jar包,下载地址) jackson-annotatio ...
- 图解JavaScript原型和原型链
先看看最简单的栗子: //构造函数 function People(name, age){ this.name = name; this.age = age; } //原型对象(所有由构造函数实例而来 ...
- .net通用CMS快速开发框架——问题1:Dapper通用的多表联合分页查询怎么破?
最近在弄一个东东,类似那种CMS的后台管理系统,方便作为其它项目的初始化框架用的. 现在遇到个问题,如标题所示:Dapper通用的多表联合分页查询怎么破? 难道只能通过拼接sql或者使用存储过程吗?我 ...
- Jmeter(十七)_驱动浏览器做GUI测试
jmeter不光可以完成性能测试.接口测试,现在也可以依靠WebDriver来完成GUI的功能自动化测试了,是不是很神奇? 1:下载JMeterPlugins-WebDriver-1.3.1.zip, ...
- lldb po [$view recursiveDescription]; 打印视图层次
备忘: lldb 打印视图层次: 对某一个view,比如operationBgView po [operationBgView recursiveDescription];