Android -- Properties使用
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public Properties loadConfig(Context context, String file) {
Properties properties = new Properties();
try {
FileInputStream s = new FileInputStream(file);
properties.load(s);
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}
public void saveConfig(Context context, String file, Properties properties) {
try {
FileOutputStream s = new FileOutputStream(file, false);
properties.store(s, "");
} catch (Exception e){
e.printStackTrace();
}
}
在Android中,比起用纯字符串读写并自行解析,或是用xml来保存配置,Properties显得更简单和直观,因为自行解析需要大量代码,而xml的操作又远不及Properties方便。
写
Properties prop = new Properties();
prop.put("prop1", "abc");
prop.put("prop2", 1);
prop.put("prop3", 3.14);
saveConfig(this, "/sdcard/config.dat", prop);
读
Properties prop = loadConfig(this, "/sdcard/config.dat");
String prop1 = prop.get("prop1");
注:也可以用Context的openFileInput和openFileOutput方法来读写文件
此时文件将被保存在 /data/data/package_name/files下,并交由系统统一管理
用此方法读写文件时,不能为文件指定具体路径。
我是天王盖地虎的分割线
Android -- Properties使用的更多相关文章
- Android Properties 存储
1.初始化 private static void initProperties(){ File logFile = new File(Constants.PROGRESS_PROPERTIES); ...
- Android SDK 5.0 这个语句带来折腾 - 生命在于折腾!
Android SDK 5.0 带来的这番折腾 - 生命在于折腾! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一 ...
- Android高效率编码-第三方SDK详解系列(三)——JPush推送牵扯出来的江湖恩怨,XMPP实现推送,自定义客户端推送
Android高效率编码-第三方SDK详解系列(三)--JPush推送牵扯出来的江湖恩怨,XMPP实现推送,自定义客户端推送 很久没有更新第三方SDK这个系列了,所以更新一下这几天工作中使用到的推送, ...
- Android设计和开发系列第二篇:Action Bar(Develop—Training)
Adding the Action Bar GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.1 or higher YOU SHOULD AL ...
- 在spring boot中使用自定义的properties
1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...
- Android init介绍(上)
1. 介绍 init进程是Linux系统第一个用户进程,是Android系统应用程序的根进程,即1号进程(PID为1):Android中的init文件位于/init,代码位于system/core/i ...
- Android官方文档翻译 十 2.3Styling the Action Bar
Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...
- 【Xamarin报错】AndroidManifest.xml : warning XA0101: @(Content) build action is not supported
部署xamarin.forms android时报错: Android\Properties\AndroidManifest.xml : warning XA0101: @(Content) buil ...
- 用Eclipse+ADT创建可运行项目,创建lib项目,引用一个lib项目
Managing Projects from Eclipse with ADT In this document Creating an Android Project 创建可运行项目 Settin ...
随机推荐
- iOS开发之网络编程--使用NSURLConnection实现大文件下载
主要思路(实现下载数据分段写入缓存中) 1.使用NSURLConnectionDataDelegate以及代理方法.2.在成功获取响应的代理方法中,获得沙盒全路径,并在该路径下创建空文件和文件句柄.3 ...
- Sonar代码质量管理工具
最近上线了,Sonar代码扫描工具: 与jenkins集成: 实现自动扫描: 下面来简单聊聊Sonar能解决什么问题: ---------------------- Sonar简介 Sonar是一个用 ...
- 附带详细注释的log4net的app.config文件配置例子
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- 【Other】U盘FAT32转NTFS且无数据丢失
序: 做了一个U盘启动盘后发现文件系统格式为FAT32.这种格式支持单个文件最大4G,超过4G就无法拷贝了.为了防止以后突发情况所以提前把FAT32转换成NTFS.为避免导入导出数据最简单的方法利用D ...
- ASP.NET Session的共享
注: 在ashx文件中使用Session 首先添加引用 using System.Web.SessionState; 实现接口 public class XXXX: IHttpHandler ==&g ...
- Requirejs2.0笔记
API http://requirejs.org/ RequireJS 插件 http://requirejs.org/docs/api.html#plugins ①require.js脚本的异步加载 ...
- Struts2 Spring Hibernate等各个版本下载推荐
推荐jar包下载地址: http://mvnrepository.com/ 应有尽有
- 由IP和掩码计算广播地址
public static IPAddress GetBroadcast(IPAddress ipAddress, IPAddress subnetMask) { var ip = ipAddress ...
- 读书笔记——Windows环境下32位汇编语言程序设计(5)模态对话框
资源可以用VC之类的生成,然后拷贝出来. 例如:每一个MFC工程都有一个resource.h,没有做任何修改时,这个resource.h文件是原来自带的.当对资源进行过修改添加之类的时,新添加的资源的 ...
- cxf 消息寻址
一.消息寻址 WS-Addressing是将消息路由数据包含在SOAP头中的一种标准方法.利用WS-Addressing的消息可以在标准化的SOAP头中包含自己的包含发送元数据,而不是依赖于网络层传输 ...