Android 文件操作心得体会

简介: android 的文件操作说白了就是Java的文件操作的处理。所以如果对Java的io文件操作比较熟悉的话,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类,这一点是很重要的,如果只是单纯的使用字符串来读取存储的文件的话,就会因为序列化的问题而出现不了目标数据。
  • 使用了返回值来对操作的结果进行了“反馈”,方便为用户提供友好的界面和使用体验。

核心


分层的思想,不同的功能的类放置到不同的包内,这样既方便程序的调试,也方便今后的代码的维护。

目录
相关文章
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
30 0
|
4月前
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
129 0
|
6月前
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(二)
android 前端常用布局文件升级总结(二)
|
2月前
|
JSON Java Go
|
2月前
|
算法 Java Android开发
安卓逆向 -- 调用其他APK的SO文件
安卓逆向 -- 调用其他APK的SO文件
17 0
|
2月前
|
Android开发
安卓逆向 -- Hook多个dex文件
安卓逆向 -- Hook多个dex文件
18 1
|
3月前
|
IDE 开发工具 Android开发
Android Studio 下发布项目成APK文件
Android Studio 下发布项目成APK文件
117 1
|
7月前
|
Android开发
Android系统开发中产品信息文件说明
Android系统开发中产品信息文件说明
80 1
|
3月前
|
安全 Android开发 数据安全/隐私保护
安卓逆向 -- SO文件逆向分析
安卓逆向 -- SO文件逆向分析
19 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
56 0