android 中使用文件进行存储程序数据

注意,读写 sdcard 时,需要用到权限,具体代码如下

xml

<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="vertical" >

< TextView
android:layout_width ="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello" />
< EditText
android:id ="@+id/et"
android:layout_height
="wrap_content"
android:layout_width
="fill_parent"
android:gravity
="left"
/>
< Button
android:id ="@+id/btnr"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="read"
/>
< Button
android:id ="@+id/btnw"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="write"
/>
< Button
android:id ="@+id/btnsd"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="writetosdcard"
/>
< Button
android:id ="@+id/btnwpro"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="writepro"
/>
< Button
android:id ="@+id/btnrpro"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="readpro"
/>
</ LinearLayout >

java 代码

packagezziss.android.filetest;

importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Properties;

importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;

public classFiletestActivity extendsActivity implementsView.OnClickListener{
/** Calledwhentheactivityisfirstcreated. */
privateEditTextet;
privateButtonbtnr;
privateButtonbtnw;
privateButtonbtnwsd;
privateButtonbtnwpro;
privateButtonbtnrpro;
@Override
public voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

et=(EditText) this.findViewById(R.id.et);
btnr=(Button) this.findViewById(R.id.btnr);
btnw=(Button) this.findViewById(R.id.btnw);
btnwsd=(Button) this.findViewById(R.id.btnsd);
btnwpro=(Button) this.findViewById(R.id.btnwpro);
btnrpro=(Button) this.findViewById(R.id.btnrpro);
btnw.setOnClickListener( this);
btnr.setOnClickListener( this);
btnwsd.setOnClickListener( this);
btnwpro.setOnClickListener( this);
btnrpro.setOnClickListener( this);
}
@Override
public voidonClick(Viewv){
// TODOAuto-generatedmethodstub
if(v.getId()==btnw.getId())
{
try{
FileOutputStreamfot=openFileOutput("fc.dat",MODE_PRIVATE);
fot.write(et.getText().toString().getBytes());
fot.close();
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
} catch(IOExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
}
}

if(v.getId()==btnr.getId())
{
try{
FileInputStreamfot=openFileInput("fc.dat");
ByteArrayOutputStreambaos= newByteArrayOutputStream();
byte[]str= new byte[1024];
intlen=-1;
while((len=fot.read(str))>-1)
{
baos.write(str,0,len);
}
et.setText(baos.toString());
fot.close();
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
} catch(IOExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
if(v.getId()==btnwsd.getId())
{
// 查找sdcard状态
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{

Fileff= this.getFileStreamPath("fc.dat");
Filefpath=Environment.getExternalStorageDirectory();
Fileft= newFile(fpath,"fc.dat");

try{
FileOutputStreamfos= newFileOutputStream(ft);
FileInputStreamfis= newFileInputStream(ff);
intlen=-1;
byte[]buf= new byte[1024];
while((len=fis.read(buf))>-1)
{
fos.write(buf,0,len);
}
fos.close();
fis.close();

} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
} catch(IOExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
}
}
} // endwritetosdcard

if(v.getId()==btnwpro.getId())
{
Propertiespro= newProperties();
pro.put("text",et.getText().toString());

try{
FileOutputStreamfos= this.openFileOutput("fc.dat",MODE_PRIVATE);
pro.store(fos,"");
fos.close();
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
} catch(IOExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
}
pro= null;
} // endwritepro
if(v.getId()==btnrpro.getId())
{
Propertiespro= newProperties();
try{
FileInputStreamfis= this.openFileInput("fc.dat");
pro.load(fis);
fis.close();
et.setText(pro.getProperty("text"));
} catch(FileNotFoundExceptione){
// TODOAuto-generatedcatchblock
Toasttoast=Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT);
toast.show();
} catch(IOExceptione){
// TODOAuto-generatedcatchblock
e.printStackTrace();
}

} // endreadpro

}
}

androidMain.xml

<? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="zziss.android.filetest"
android:versionCode
="1"
android:versionName
="1.0" >

< uses-sdk android:minSdkVersion ="14" />
<!-- 添加上读写存储卡 -->
< uses-permission android:name ="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
< uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE" />

< application
android:icon ="@drawable/ic_launcher"
android:label
="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name
=".FiletestActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />

< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >

</ manifest >

更多相关文章

  1. Android 对话框【Dialog】去除白色边框代码
  2. 介绍一下android的各种权限。 代码如下: Html代码 <manifestxmlns
  3. android aosp 下载源代码
  4. 《android开发应用实战详解》光盘源代码
  5. Android 权限管理 — 只防君子不防小人

随机推荐

  1. 【原创】如何在Android中为TextView动态
  2. Android万能适配器Adapter
  3. android 普通对话框
  4. Android(安卓)-- 获取汉字的首字母
  5. android中调用.net web service
  6. create new Android(安卓)Virtual Device
  7. 在线安装ADT总是出现以下错误
  8. android如何编差分包升级
  9. Android实现邮箱验证功能
  10. 为android应用程序添加桌面快捷方式