类 Properties
Properties是hashtable的子类。
也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。
是集合中和IO技术相结合的集合容器。
该对象的特点:可以用于键值对形式的配置文件。
那么在加载数据时,需要数据有固定格式:键=值。
java.lang.Object |--java.util.Dictionary<K,V> |--java.util.Hashtable<Object,Object> |--java.util.Properties
public class Properties
extends Hashtable< Object, Object>
常用方法:
String | getProperty(String key) 用指定的键在此属性列表中搜索属性。 |
String | getProperty(String key,String defaultValue) 用指定的键在属性列表中搜索属性。 |
Object | setProperty(String key,String value) 调用 Hashtable 的方法 put 。 |
void | list(PrintStream out) 将属性列表输出到指定的输出流。 |
void | list(PrintWriter out) 将属性列表输出到指定的输出流。 |
void | load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。 |
void | load(Reader reader) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。 |
Set<String> | stringPropertyNames() 遍历集合返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。 |
import java.util.*;
class PropertiesDemo
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
SetAndGet();
}
public static void SetAndGet()
{
Properties pro = new Properties();
pro.setProperty("honghong","30");
pro.setProperty("fangfang","38");
String value = pro.getProperty("honghong");
sop(value);
Set<String> ss = new HashSet<String>();
ss = pro.stringPropertyNames();//返回键的集合
sop(ss);
}
}
演示,如何将流中的数据存储到集合中。
想要将info.txt中键值数据存到集合中进行操作。
1,用一个流和info.txt文件关联。
2,读取一行数据,将该行数据用"="进行切割。
3,等号左边作为键,右边作为值。存入到Properties集合中即可。
使用到String类中的方法:
String[] | split(String regex) 根据给定正则表达式的匹配拆分此字符串。 |
import java.util.*;
import java.io.*;
class PropertiesDemo
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args) throws IOException
{
method_1();
}
/*
演示,如何将流中的数据存储到集合中。
想要将info.txt中键值数据存到集合中进行操作。
1,用一个流和info.txt文件关联。
2,读取一行数据,将该行数据用"="进行切割。
3,等号左边作为键,右边作为值。存入到Properties集合中即可。
*/
public static void method_1()throws IOException
{
//源:纯文本FileReader、BufferedReader
BufferedReader br = new BufferedReader(new FileReader("info.txt"));
String line = null;
Properties pro = new Properties();
while((line=br.readLine())!=null)
{
String[] arr = line.split("=");
pro.setProperty(arr[0],arr[1]);
}
br.close();
sop(pro);
}
}
演示load、list方法:
import java.util.*;
import java.io.*;
class PropertiesDemo
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args) throws IOException
{
loadDemo();
}
public static void loadDemo()throws IOException
{
//使用字节流与Properties关联。
FileInputStream fis = new FileInputStream("info.txt");
Properties prop = new Properties();
prop.load(fis);
sop(prop);
//使用字符流与Properties关联。
FileReader fr = new FileReader("info.txt");
Properties prop1 = new Properties();
prop1.load(fr);
//sop(prop1);
//不一样的打印方式:
prop1.list(System.out);
}
}
void | store(OutputStream out,String comments) 以适合使用 load(InputStream) 方法加载到Properties 表中的格式,将此Properties 表中的属性列表(键和元素对)写入输出流。 |
void | store(Writer writer,String comments) 以适合使用 load(Reader) 方法的格式,将此Properties 表中的属性列表(键和元素对)写入输出字符。 |
import java.util.*;
import java.io.*;
class PropertiesDemo
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args) throws IOException
{
loadDemo();
}
public static void loadDemo()throws IOException
{
//使用字节流与Properties关联。
FileInputStream fis = new FileInputStream("info.txt");
Properties prop = new Properties();
prop.load(fis);
sop(prop);
//使用字符流与Properties关联。
FileReader fr = new FileReader("info.txt");
Properties prop1 = new Properties();
prop1.load(fr);
//sop(prop1);
//不一样的打印方式:
prop1.list(System.out);
//修改文件中的年龄
prop1.setProperty("zhaoqi","28");
prop1.list(System.out);//打印结果变了,可是文件中的结果没变
//将改变了的年龄数据写入到文件中
FileWriter fw = new FileWriter("info.txt");
prop1.store(fw,"haha");//"haha"为注释信息。
fis.close();
fr.close();
fw.close();
}
}
凡是前面带着#的,都是注释信息,不会被Properties所加载。
Properties加载的信息必须是键值对,不是键值对它加载它没意义。
用于记录应用程序运行次数。
如果使用次数已到,那么给出注册提示。
很容易想到的是:计数器。
可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。
可是随着该应用程序的退出,该计数器也在内存中消失了。
下一次在启动该程序,又重新开始从0计数。
这样不是我们想要的。
程序即使结束,该计数器的值也存在。
下次程序启动在会先加载该计数器的值并加1后在重新存储起来。
所以要建立一个配置文件。用于记录该软件的使用次数。
该配置文件使用键值对的形式。
这样便于阅读数据,并操作数据。
键值对数据是map集合。
数据是以文件形式存储,使用io技术。
那么map+io -->properties.
配置文件可以实现应用程序数据的共享。
import java.util.*;
import java.io.*;
class RunCount
{
public static void main(String[] args) throws IOException
{
Properties prop = new Properties();
File file = new File("count.ini");
if(!file.exists())
file.createNewFile();
FileReader fr = new FileReader(file);
prop.load(fr);
int count = 0;
String value = prop.getProperty("time");
if(value!=null)
{
count = Integer.parseInt(value);
if(count>=5)
{
System.out.println("使用次数已到,拿钱!");
return;
}
}
count++;
prop.setProperty("time",count+"");
FileWriter fw = new FileWriter(file);
prop.store(fw,"");
fr.close();
fw.close();
}
}
配置文件都是以.properties或者.xml结尾的,或者.ini
.properties是以键值对形式存放数据的,如果数据关系太复杂的,它存放不了
可以用xml来存放,例如:
<persons>
<person id="001">
<name>zhagnsan</name>
<age>30</age>
<address>bj</address>
</person>
<person>
<name
</person>
</persons>
这里面数据封装的这么复杂,怎么取呢?
Java本身就有取这东西的对象:org.w3c.dom 这个包里面有个 接口:Document 表示整个 HTML 或 XML 文档
可是没有想到Java来访问这些对象特别麻烦,
所以又一帮牛人们做了个更简单的工具,叫做 dom4j
所谓 dom4j 就是:dom for java 的意思
这个工具等以后开发到XML的时候就要用了。
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net