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 ...
随机推荐
- java实现栈和队列
class Node { int val; Node next; Node(int x) { val = x; next = null; } } class Stack { Node top; pub ...
- C语言-03-流程控制
一.选择结构 1> if语句 使用注意 ① if语句中的条件语句,不要把==和=弄混,可以把常量作为左值, 这样的话,在无用=的情况下,编译时会报错 ② if语句后若要定义新的变量或者有多条语句 ...
- Windows平台的Eclipse-javaEE-mars相关配置
平台:winddow10 前提: 1> 搭建好了jdk1.8.0_60环境 2> 下载放置好了apache-tomcat-8.0.24 3> 下载好了eclipse-jee-m ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Sqlserver创建连接MySql的链接服务器
第一步:在MySql服务器上安装与系统对应的 MySql-Connector-ODBC 官方下载地址 安装过程中可能会报 缺失 msvcr100.dll 的错误,这需要你根据系统到网上下载对应的这个 ...
- C语言的数据类型及其对应变量
声明,定义和初始化 声明标识符iden是告诉编译器"有这么一个变量var,具体var里是什么,你自己去看".声明只需要标识符的类型和标识符名字,C语言的任何标识符在使用前都需要声明 ...
- nyoj 1029/hdu 3714 Error Curves 三分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3714 懂了三分思想和F(x)函数的单调性质,这题也就是水题了 #include "stdio ...
- jQuery事件绑定on、off 和one,取代bind, live, delegate
jQuery最新版建议:最好用on来代替以前的bind, live, delegate,其中live是最不建议使用的. on和off的格式 on $(elements).on(events[, sel ...
- Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)
1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...
- 【MVC 4】4.MVC 基本工具(Visual Studio 的单元测试、使用Moq)
作者:[美]Adam Freeman 来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本 ...