昨天想写一个记账本,发现并不能把项目名称与内容关联起来,于是乎我想到了map,可是又不知道map储存到文件中又怎么读出来,幸好今天遇到了properties
Properties是Hashtable的子类,Hashtable实现Map接口
所以Properties其实是一个Map
Map(映射)
首先Map是一个接口,所以实例化找他的子类HashMap、LinkedHashMap和TreeMap
不同于List和Set的是,Map并不实现Collection,所以不能用Collections的方法
Map方法
构造函数
和以前一样
Map
添加
put(Key key,Value value):void
putAll(Map xxxx):void复制映射
相当与容器中的add()和addAll()
此时如果存在旧的键值对,并不是添加,而是覆盖
得到
get(Key key):Value 若该键不存在,则返回null,相当于Collection中的get(索引)
keySet():Set 返回Key的Set视图,因为key不可重复,所以返回不可重复的Set视图,而不是List
values():Collection 返回Value的Collection(实际是List)视图,因为Value可以重复,所以返回可以重复的List视图
entrySet():Set
判断
isEmpty():boolean
containsKey(Key key):boolean
containsValue(Value value):boolean
操作
clear():void 清空
remove(Key key):Value 返回被删除的值
size():int 返回键值对的数量
hashCode():int 返回映射中各项哈希值之和
equals(Map another):boolean 比较两个引用是否相等
看到这里,我们发现在Map接口中重写了java.lang.Object中的equals(重写成各项哈希值之和)和hashCode()方法
使得equals是比较内容,而不是比较引用
又!是在Map中重写的,所以对Map下的所有子类之间都可以用equals去比较!
Properties(属性列表)
Properties的方法
构造方法
Properties():
Properties(Properties default):
因为Properties中有一个字段Properties defaults,这是一个默认的属性列表,包含未找到的默认值,所以第二的构造方法,就把default作为默认属性列表
添加
因为Properties必须添加
得到
getProperty(String key):String 得到值
stringPropertySet():Set与Set转换
写出去
store(OutputStream out,String comments):void 用字节流输出
store(Writer out,String comments):void 用字符流输出
参数comments是注释,会出现的文件的首行,格式#comments
不过注释千万不要写中文,因为好像是编码的原因,中文会出现乱码
list(PrintStream out):void
list(PrintWriter out):void
将列表属性输出到指定的输出流可以用来打印
目前我还没知道有什么作用
注意:上面两个方法都不输出其默认属性列表
写入各个项后,自动刷新输出流。此方法返回后,输出流仍保持打开状态。
读进来
load(InputStream in):void
load(Reader):void
和写进来的方法一样,此方法返回后,指定的流仍保持打开状态。
对应相应输出时候的格式(字节or字符)
用Properties的好处
输出到文本的时候带有时间(相当于打印date对象),且自动换行
输入的时候自动识别键和值
因为值的数据类型是String,所以如果想对int进行操作就有点麻烦,这里有一个方法:
java.lang.Integer.parseInt(String) 这是一个静态的方法,将十进制的字符串转换为数字
用Properties做一个登录计数器
import java.util.*;
import java.io.*;
public class LoginAccounter{
public static void main(String[] args)throws IOException{
File accounter=new File("accounter.properties");
FileOutputStream out=new FileOutputStream(accounter,accounter.isFile());
FileInputStream in=new FileInputStream(accounter);
Properties account=new Properties();
account.load(in);
int time=Integer.parseInt(account.getProperty("Time","0"));
if((++time)<3){
System.out.println("少侠,风尘仆仆,梦幻西游一直在您左右,我们为您准备了在线大礼包,请听我道来\r\n您已经来到梦幻西游"+time+"次了,再登录"+(3-time)+"次,您就可以获得**在线大礼包**!!!");}
else{System.out.println("少侠,您已经满足领取在线大礼包(登录10次)的条件了,请少侠移步至梦幻精灵处领取!!感谢您支持梦幻西游!");}
account.setProperty("Time",(time)+"");
account.store(out,"LoginAccounter");
}}
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
spring+sendmail+模板+properties+vm 包含类
c3p0+xml+jdbc.properties+DBUtils+mysql-connector
antisamy-esapi.xml+ESAPI.properties+validation.properties
读取properties文件返回map
读取properties返回map并写入文件
在Linux上安装jsp运行环境时,会整合apache和tomcat,其中需要的两个文件,wokers2.properties和jk2.properties 就在此处
NULL 博文链接:https://meng-sicheng.iteye.com/blog/1696594
关于java 一些常用文件的读取和写入硬盘!
To overcome these drawbacks, in this paper, we extend the Logistic map-3 to the finite field Z 3 n , and give theoretical analysis about some period properties of the Logistic map-3 over Z 3 n ....
汉字转换为ascii码用命令:native2ascii.exe 本程序可以正常运行~~~ http://blog.csdn.net/facepp/archive/2008/05/16/2451150.aspx
log4j.properties+log4j日志文件
Spring 读取properties文件key+value方式.rar
本文侧重于了解自定义properties文件,以及properties中的数据格式,在gradle中如何使用等知识点。 一、概述 在Android Studio 创建一个项目的时候,rootProject下面会生成gradle.properties和local.properties文件...
Physical Properties of CrystalsPhysical Properties of CrystalsPhysical Properties of CrystalsPhysical Properties of Crystals
apache+tomcat集群配置 tomcat 中workers.properties配置说明
[ ] eigenbase-properties-1.1.4-javadoc.jar 06-Dec-2013 18:28 83K [ ] eigenbase-properties-1.1.4-javadoc.jar.md5 06-Dec-2013 18:28 32 [ ] eigenbase-properties-1.1.4-javadoc.jar.sha1 06-Dec-2013 18:28...
native2ascii 是一个本地码到 ASCII 码转换器,可以将含有本地编码字符的文件转换为 Unicode 编码字符的文件。 使用 native2ascii 工具可以将 Properties 文件转换为 Unicode 编码字符的文件,然后将转换后的文件...
怎样读取properties文件内容,用java中最基本的方式去读取properties文件中的内容
js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。
eclipse下properties文件编辑插件