ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

Android 文件操作心得体会

来源:网络整理     时间:2016-04-11     关键词:Android

本篇文章主要介绍了"Android 文件操作心得体会",主要涉及到Android方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: android 的文件操作说白了就是Java的文件操作的处理。所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了。好了,话不多...

android 的文件操作说白了就是Java的文件操作的处理。所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了。好了,话不多说,开始今天的正题吧。


先从一个小项目入门吧


首先是一个布局文件,这一点比较的简单,那就直接上代码吧。

<LinearLayoutxmlns: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="文件名称" /><EditTextandroid: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="文件内容" /><EditTextandroid:id="@+id/et_filecontent"android:layout_width="match_parent"android:layout_height="wrap_content"android:lines="7"android:hint="file content"
        /><Buttonandroid:id="@+id/btn_save"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="toSave"android:text="Save"
        /><Buttonandroid: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;

publicclassMainActivityextendsActivity {private EditText mEt_filename,mEt_filecontent;
    private Button mBtn_save;

    privatevoidinit(){
        mEt_filecontent = (EditText) findViewById(R.id.et_filecontent);
        mEt_filename = (EditText) findViewById(R.id.et_filename);
        mBtn_save = (Button) findViewById(R.id.btn_save);
    }

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    /**
     * 保存数据到一个文件中
     * @param view
     */publicvoidtoSave(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();
        }
    }

    publicvoidgetFile(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;

publicclassFileService {//android自带的可以快速获得文件输出流的一个类,注意参数不能是路径,只能是文件名称private Context mContext;

    publicFileService(Context context) {
        this.mContext = context;
    }

    /**
     * 保存文件的一个方法
     * @param fileName
     * @param fileContent
     * @return
     */publicbooleansave(String fileName, String fileContent) {
        try {
            //采用Context.MODE_PRIVATE模式的话,只允许本应用访问此文件,并且熟覆盖式的添加数据
            FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
            fos.write(fileContent.getBytes());
            fos.close();
            returntrue;
        } catch (Exception e) {
            e.printStackTrace();
            returnfalse;
        }

    }

    /**
     * 获得之前保存过的文件的详细的信息
     * @param fileName
     * @return
     */public String getFile(String fileName) {
        String fileContent = "";
        try{

            FileInputStream fis = mContext.openFileInput(fileName);
            byte[] buf = newbyte[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类,这一点是很重要的,如果只是单纯的使用字符串来读取存储的文件的话,就会因为序列化的问题而出现不了目标数据。
  • 使用了返回值来对操作的结果进行了“反馈”,方便为用户提供友好的界面和使用体验。

核心


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

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介绍了Android 文件操作心得体会,包括了Android方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

    本文网址链接:http://www.codes51.com/article/detail_647367.html

    上一篇337 House Robber III 下一篇hibernate 缓存

    相关图片

    相关文章