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则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...
随机推荐
- VLAN的划分
VLAN划分是指逻辑上把网络资源和网络用户按照一定的原则进行划分,把一个物理上实际的网络划分成多个小的逻辑网络.设计VLAN的最初目的是隔离局域网的广播,不让它去影响网络带宽.VLAN与传统的LAN相 ...
- CSS中的ul与li样式详解 list-type
转自新浪博客http://blog.sina.com.cn/u/2539885750 ul和li列表是使用CSS布局页面时常用的元素.在CSS中,有专门控制列表表现的属性,常用的有list-style ...
- URLconnection
URLconnection 开发中最常用的两种网络请求: 1.标准Java接口(java.NET) --HttpURLConnection,可以实现简单的基于URL请求.响应功能: 2.Apache ...
- ASP.NET Core 添加统一模型验证处理机制
一.前言 模型验证自ASP.NET MVC便有提供,我们可以在Model(DTO)的属性上加上数据注解(Data Annotations)特性,在进入Action之前便会根据数据注解,来验证输入的数据 ...
- 【python标准库模块三】Os模块和Sys模块学习
Os模块 导入os模块 import os 获取当前工作目录 os.getcwd() 切换目录,跟linux中的cd一样 os.chdir("文件夹名") 递归生成文件夹 os.m ...
- ACM 饭卡
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...
- Node.js HTTPS
稳定性: 3 - 稳定 HTTPS 是基于 TLS/SSL 的 HTTP 协议.在 Node 里作为单独的模块来实现. 类: https.Server 这是 tls.Server 的子类,并且和 ht ...
- linux系统性能监控--网络利用率
Linux中提供了许多有助于评估各种 Linux网络性能的监视工具,其中一些监视工具也可用于解决网络问题以及监视性能. Linux内核为用户提供了大量的网络系统信息,这有助于监视网络的健康状态并检测在 ...
- 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果
效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...
- Android中Sqlite数据库进行增删改查
今天这篇文章写Sqlite数据库,通过一个小案例来完整讲一下数据库常见的CRUD操作. 先对知识点总结: SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHel ...