且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android中的数据存取-方式二:file(文件)

更新时间:2022-10-03 10:16:34

在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

数据读取

public static String read(Context context, String file) {
String data = "";
try {
FileInputStream stream = context.openFileInput(file);
StringBuffer sb = new StringBuffer();
int c;
while ((c = stream.read()) != -1) {
sb.append((char) c);
}
stream.close();
data = sb.toString();

} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return data;
}

从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package***享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。

数据写入

public static void write(Context context, String file, String msg) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
stream.write(msg.getBytes());
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:

public static Properties load(Context context, String file) {
Properties properties = new Properties();
try {
FileInputStream stream = context.openFileInput(file);
properties.load(stream);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return properties;
}

public static void store(Context context, String file, Properties properties) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
properties.store(stream, "");
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
 
/Chapter09_Data_02/src/com/amaker/test/MainActivity.java

 


  1. 代码package com.amaker.test;  
  2.  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5.  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.  
  13. public class MainActivity extends Activity {  
  14.     private static final String FILE_NAME="temp.txt";  
  15.     private Button b1,b2;  
  16.     private EditText et1,et2;  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         b1 = (Button)findViewById(R.id.Button01);  
  22.         b2 = (Button)findViewById(R.id.Button02);  
  23.           
  24.         et1 = (EditText)findViewById(R.id.EditText01);  
  25.         et2 = (EditText)findViewById(R.id.EditText02);  
  26.           
  27.         b1.setOnClickListener(new OnClickListener() {  
  28.             @Override 
  29.             public void onClick(View v) {  
  30.                 write(et1.getText().toString());  
  31.             }  
  32.         });  
  33.           
  34.         b2.setOnClickListener(new OnClickListener() {  
  35.             @Override 
  36.             public void onClick(View v) {  
  37.                 et2.setText(read());  
  38.             }  
  39.         });  
  40.     }  
  41.       
  42.     private String read(){  
  43.         try {  
  44.             FileInputStream fis = openFileInput(FILE_NAME);  
  45.             byte[] buffer = new byte[fis.available()];  
  46.             fis.read(buffer);  
  47.             return new String(buffer);  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.         return null;  
  52.     }  
  53.       
  54.     private void write(String content){  
  55.         try {  
  56.             FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);  
  57.             fos.write(content.getBytes());  
  58.             fos.close();  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.     }  
  63. }  
  64.  
  65.  
  66.    
  67. /Chapter09_Data_02/res/layout/main.xml 

 


  1. 代码<?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent">  
  5.       
  6.     <TextView android:layout_width="fill_parent" 
  7.         android:layout_height="wrap_content" android:text="File Test" />  
  8.           
  9.     <EditText android:text="" android:id="@+id/EditText01" 
  10.         android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="100px"></EditText>  
  11.           
  12.     <Button android:id="@+id/Button01" android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" android:text="Write"></Button>  
  14.           
  15.     <EditText android:text="" android:id="@+id/EditText02" 
  16.         android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="100px"></EditText>  
  17.           
  18.     <Button android:id="@+id/Button02" android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" android:text="Read"></Button>  
  20.           
  21. </LinearLayout>  
  22.  
  23.  
  24.  
  25.    
  26. /Chapter09_Data_02/AndroidManifest.xml 

  1. 代码<?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.amaker.test" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity" 
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.  
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="3" />  
  17.  
  18. </manifest> 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079365