- 浏览: 28005 次
- 性别:
- 来自: 北京
最新评论
-
xiaojianhx:
Environment 是哪个包里面的?我开发4.0,用不了这 ...
Android 检测SD卡应用 -
qianlei541:
不错不错不错
android软键盘enter键的替换与事件监听 -
ihopethatwell:
到处都是转载的,楼主,你这个图片这个调用能贴上?
android yuv摄像 -
mikite:
mark,看看
Android中的动画研究 -
anyang763:
多谢lz分享,学习了!
android系统调用
android 读取文件内容操作
在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。
数据读取
view plaincopy to clipboardprint?
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 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。
数据写入
view plaincopy to clipboardprint?
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) {
}
}
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来使用:
view plaincopy to clipboardprint?
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 读取文件内容操作2
读:
public String ReadSettings(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
写:
public void WriteSettings(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用方法:
WriteSettings(this,"setting0, setting1, setting2");
String data[] = ReadSettings(this).split(",");
发表评论
-
android 的 Linkify
2011-12-29 09:03 1168Linkify是一个辅助类,通过RegEx样式匹配,自动地在T ... -
Android教程之AndroidManifest.xml文件详介绍及解析
2011-12-21 16:58 0一、关于AndroidManifest.xml Android ... -
obj文件应用
2011-12-20 09:27 0OBJ文件格式(转http://www.ategpu.com/ ... -
Android 应用程序多Activity跳转之后退出整个程序
2011-12-13 11:48 867http://txlong-onz.iteye.com/blo ... -
Android 检测SD卡应用
2011-12-13 10:21 7039if (Environment.getExternalStor ... -
android yuv摄像
2011-12-13 09:26 2873http://eyehere.net/2011/android ... -
Android拍摄视频流的格式转换(YUV --- RGB)
2011-12-13 08:56 1818Android允许用户实时捕获摄像头的视频流,这在利用摄像头的 ... -
android软键盘enter键的替换与事件监听
2011-12-12 09:03 3480软件盘的界面替换只有一个属性android:imeOption ... -
android listView
2011-12-06 09:11 0一、 1 listview中在设置了背景之后,会发生listv ... -
Android 即时通讯 XMPP
2011-12-05 09:47 0http://www.cnblogs.com/charley_ ... -
搭建XMPP协议,实现自主推送消息到手机
2011-12-05 09:46 0http://www.devdiv.com/article-2 ... -
xmpp协议实现消息推送
2011-12-05 09:43 0http://www.cnblogs.com/luxiaofe ... -
Android推送方式比较
2011-12-05 09:32 0http://blog.csdn.net/xyz_lmn/ar ... -
Android教程之AndroidManifest.xml文件详介绍及解析
2011-12-02 09:25 0Android教程之AndroidManifest.xml文 ... -
Android用GSon处理Json数据
2011-12-02 08:57 4http://www.cnblogs.com/VinC/arc ... -
Android访问WCF服务---服务端开发
2011-12-02 08:45 0Android访问WCF服务(上篇)-服务端开发 本章目的: ... -
高效率下载图片——防止内存溢出
2011-12-01 12:06 0在应用中经常需要下载很多的图片,因此,写好图片下载部分的代码非 ... -
ScrollListener
2011-12-01 12:00 0new OnScrollListener() { bo ... -
Android虚拟机(DVM)内存分配——内存溢出问题
2011-12-01 11:57 0大家都知道Android的上层应用是基于 Dalvik Vir ... -
bitmap溢出处理
2011-12-01 11:40 0当从SD卡中获取图片时,如果图片太大,加载会出现溢出异常。因此 ...
相关推荐
android读取文件内容操作.pdf
android 读取文件内容操作
Android - 文件读写操作 方法总结
Android 读写文件实例,包括 SD 卡读写和 内部存储读写。 文件目录获取等。
android 通过Environment环境 获取手机根目录 读写 文件操作。
Android jni文件读写 NDK文件读取 读写 操作 NDK数据加密DEMO 源码直接上传,运行在android6.0系统测试通过
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
Android SD卡文件读写操作可以学习下
android 写了一个工具类实现txt文件保存和读取
实时检测USB的插入,检测并读取文件,也可根据路径进行文件读写操作
android的IO流读写文件操作示意程序
文件操作工具类,包含生成保存,复制,删除,读取,获取文件名,获取文件列表等等,只有你想不到,没有你找不到的Android端工具类,复制到项目中可直接使用
Android文件读写操作总结
该压缩包中含有两个android工程:file和other。file中主要讲述了对文件的保存与读取的功能,other主要用来测试文件保存的四种操作模式。
Android读写文件,该文件包含Android里的读写txt文件,方便初学者使用同时欢迎大家交流经验@!
1.对读写文件操作的练习 2.对SQLite读写数据库的操作练习。 3.对ListView的练习 4.代码清楚,层次明了。 android sdk 2.3.*
android开发的文件读写,简单操作。适合初学者参考。
android文件的创建、删除、 读写、 判断是否操作、文件MD5 java基础学习型
android 读写文件的工具类,对读写操作进行了封装。