android中的数据存取-方式二:file(文件)
2011-01-22 17:21
465 查看
在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。
数据读取
从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。
数据写入
在这里打开文件的时候,声明了文件打开的方式。
一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:
[/code]
数据读取
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
代码[code]<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amaker.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".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> <uses-sdk android:minSdkVersion="3" /> </manifest>
[/code]
相关文章推荐
- android中的数据存取-方式二:file(文件)
- Android学习 —— 数据的存储与访问方式一: 文件存取
- Android学习 —— 数据的存储与访问方式一: 文件存取
- android存取数据方式:文件、SharedPreferences
- 说说在 Android 中如何使用文件存取数据
- android的数据与访问(2)-delphi xe7如何存取我的app配置参数文件?
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File
- Android数据存储之文件 openFileOutput & openFileInput
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File 之 —— SharedPreferences
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
- Android中的数据存取三种方式
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
- Android数据存储方式之:文件存储
- android中的数据存取-方式一:preference(配置)
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览