Android 数据存取
2009-07-04 14:49
274 查看
在android文件系统中,application 文件存放在/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;
}
数据写入
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) {
}
}
数据读取
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;
}
数据写入
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) {
}
}
相关文章推荐
- Android MMS模块数据存取数据表
- Android : JSON存取数据 以及 DOM读取XML数据
- Android的Sqlite中DateTime类型数据的存取问题
- SharedPreferences数据存取操作,android引导页一次执行的方法。
- 使用云(BAE)实现android应用数据的远程存取(MySQL)
- Android数据存取 与SQLiter 之一
- Android数据存取之Databases
- android中SharedPreferences和PreferenceActivity的存取数据
- Android学习 —— 数据的存储与访问方式一: 文件存取
- Android开发学习笔记:数据存取之SQLite浅析
- Android内存和SD卡的数据存取
- Android数据存取之Databases
- 【JAVA】java中实现map集合的数据存取详解三种方法。Android程序员也是要会写的
- android中的数据存取-方式二:file(文件)
- 第60章、数据文件存取至储存卡(从零开始学Android)
- Android开辟进修之SQLite数据存取浅析
- Android数据存取之Preferences
- Android_Preference存取数据
- [Android] 数据实体的自动存取--SharedPreferences篇
- Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像