android file存储方法实例
最新推荐文章于 2024-05-02 16:22:12 发布
android file 存储方法 1. android 中的数据存储就不多说了,相信大家已经也有所了解,常见有file , simplesave ,sqlite , javaweb等,今天给大家介绍一下file。 这个实例就是用我们说的mvc框架编写的 model层代码如下: package org.meng.model;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.content.Context;public class FileService {/* * 实现文件的保存和读取 * @author 孟凡辉 * @time 2013-5-9 */private Context context;//用构造方法传一个上下文public FileService(Context context) {super();this.context=context;}//保存作用public void savaFilea(String fileName,String fileContext) throws IOException{FileOutputStream fos=context.openFileOutput(fileName, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);fos.write(fileContext.getBytes());//将上下文和文件通过输出流建立联系fos.close();}//阅读文件public String readFile(String fileName) throws IOException{FileInputStream fis=context.openFileInput(fileName);int len=0;byte[] buffer= new byte[1024]; //定义一个缓冲区大小为了存储流中的数据ByteArrayOutputStream baos= new ByteArrayOutputStream();while(fis.read(buffer)!=-1){baos.write(buffer,0,len);} //下面从内冲读取数据byte[] data=baos.toByteArray();baos.close();fis.close();return data.toString();}}控制层center(也就是逻辑处理层)package com.example.filestore;import org.meng.model.FileService;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private FileService fileService;private Button button;private EditText filenameText;private EditText filecontexttext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);filenameText= (EditText) findViewById(R.id.editText1);filecontexttext= (EditText) findViewById(R.id.editText2);button=(Button) this.findViewById(R.id.button1);}public void savefile(View view) {String fileName=filenameText.getText().toString();String fileContext=filecontexttext.getText().toString();try { fileService.savaFilea(fileName, fileContext); Toast.makeText(MainActivity.this, R.string.success,Toast.LENGTH_LONG ).show();} catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, R.string.faluse, Toast.LENGTH_LONG).show(); }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}layout 代码如下:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" android:textColor="#9F79EE" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#9F79EE" android:text="@string/filecontent" /> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="5" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="savefile" android:text="@string/savebutton" /></LinearLayout>要想测试一个方法需要在AndroidManifest.XML 配置一下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.filestore" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner" /> <activity android:name="com.example.filestore.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.filestore" > </instrumentation> android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.filestore" > </instrumentation></manifest>
//红色部分就是用测试的权限和配置!
如果有疑问就在问我吧!