Java中Properties类的操作配置文件
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>知识学而不用,就等于没用,到真正用到的时 候还得重新再学。最近在看几款开源模拟器的源码,里面涉及到了很多关于类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写,外加一些脚本语言,Perl之类的,不得已,又得重新拾起。本文通过看《Java编程思想》和一些网友的博客总结而来java properties 变量,只为简 单介绍类的相关操作。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>一、Java 类
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>Java中有个比较重要的类(Java.util.),主要用于读取Java的配置文件,各种语言都有自己所支 持的配置文件Java中Properties类的操作配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像支持的配置文件 是.ini文件,同样Java中Properties类的操作配置文件,它也有自己读取配置文件的类,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配 置文件常为.文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用”#”来注释。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>它提供了几个主要的方法:
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>1. ( key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>2. load ( ),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test. 文件)进行装载来获取该文件中的所有键 – 值对。以供 ( key) 来搜索。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>3. ( key, value) ,调用 的方法 put 。他通过调用基类的put方法来设置 键 – 值对。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>4. store ( out, ),以适合使用 load 方法加载到 表中的格式,将此 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 – 值对写入到指定的文件中去。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>253&fmt=JPEG&app=138&f=JPEG?w=912&h=513″ alt=”java properties 变量”/ style=”max-width: 100%; height: auto; display: block; margin: 1.8em auto; border-radius: 10px; box-shadow: 0 4px 20px rgba(236,72,153,0.18);”>
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>5. clear (),清除所有装载的 键 – 值对。该方法在基类中提供。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>二、Java读取文件
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>最常用的还是通过java.lang.Class类的( name)方法来实现java properties 变量,如下可以这样调用:
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”> in = ().(“资源Name”);作为我们写程序的,用此一种足够。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>或者下面这种也常用:
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”> in = new (new ());
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>三、相关实例
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>下面列举几个实例java properties 变量,加深对类的理解和记忆。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>我们知道,Java虚拟机(JVM)有自己的系统配置文件(.),我们可以通过下面的方式来获取。
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>1、获取JVM的系统属性
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>
1 import java.util.Properties;
2
3 public class GetJVM {
4 public static void main(String[] args) {
5 Properties pps = System.getProperties();
6 pps.list(System.out);
7 }
8 }
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>253&fmt=JPEG&app=138&f=JPEG?w=872&h=500″ alt=”java properties 变量”/ style=”max-width: 100%; height: auto; display: block; margin: 1.8em auto; border-radius: 10px; box-shadow: 0 4px 20px rgba(236,72,153,0.18);”>
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>2、随便新建一个配置文件(Test.)
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>
name=jack Weight=60 Height=175
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>
1 public class getProperties {
2 public static void main(String[] args) throws FileNotFoundException, IOException {
3 Properties pps = new Properties();
4 pps.load(new FileInputStream("Test.properties"));
5 Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
6 while(enum1.hasMoreElements()) {
7 String strKey = (String) enum1.nextElement();
8 String strValue = pps.getProperty(strKey);
9 System.out.println(strKey + "=" + strValue);
10 }
11 }
12 }
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>3、一个比较综合的实例
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>根据key读取value
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>读取的全部信息
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>写入新的信息
<p style="margin: 1.25em 0; line-height: 1.9; font-size: 16px; color: #222222; text-align: justify; letter-spacing: 0.02em;”>
1 //关于Properties类常用的操作
2 public class TestProperties {
3 //根据Key读取Value
4 public static String GetValueByKey(String filePath, String key) {
5 Properties pps = new Properties();
6 try {
7 InputStream in = new BufferedInputStream (new FileInputStream(filePath));
8 pps.load(in);
9 String value = pps.getProperty(key);
10 System.out.println(key + " = " + value);
11 return value;
12
13 }catch (IOException e) {
14 e.printStackTrace();
15 return null;
16 }
17 }
18
19 //读取Properties的全部信息
20 public static void GetAllProperties(String filePath) throws IOException {
21 Properties pps = new Properties();
22 InputStream in = new BufferedInputStream(new FileInputStream(filePath));
23 pps.load(in);
24 Enumeration en = pps.propertyNames(); //得到配置文件的名字
25
26 while(en.hasMoreElements()) {
27 String strKey = (String) en.nextElement();
28 String strValue = pps.getProperty(strKey);
29 System.out.println(strKey + "=" + strValue);
30 }
31
32 }
33
34 //写入Properties信息
35 public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
36 Properties pps = new Properties();
37
38 InputStream in = new FileInputStream(filePath);
39 //从输入流中读取属性列表(键和元素对)
40 pps.load(in);
41 //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
42 //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
43 OutputStream out = new FileOutputStream(filePath);
44 pps.setProperty(pKey, pValue);
45 //以适合使用 load 方法加载到 Properties 表中的格式,
46 //将此 Properties 表中的属性列表(键和元素对)写入输出流
47 pps.store(out, "Update " + pKey + " name");
48 }
49
50 public static void main(String [] args) throws IOException{
51 //String value = GetValueByKey("Test.properties", "name");
52 //System.out.println(value);
53 //GetAllProperties("Test.properties");
54 WriteProperties("Test.properties","long", "100");
55 }
56 }
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299
资源库 - 资源分享下载网 » Java中Properties类的操作配置文件
常见问题FAQ
- 关于资源售价和售后服务的说明?
- 代码有没有售后服务和技术支持?
- 有没有搭建服务?
- 链接地址失效了怎么办?
- 关于解压密码

