一.Properties简介

Properties 类继承自HashTable,提供的方法很像Map的实现类HashMap。它在 Java 编程的早期就有了,并且几乎没有什么变化。J2SE 的 Tiger 版本增强了这个类,不仅可以用它在单独一行中指定用等号分隔的多个键-值对(其中键和值是用等号分隔的),还可以用XML 文件装载和保存这些键-值对

二.Properties 的加载和简单的使用

1.路径存放

2.信息配置

name = Lily
age = 22

3.简单的加载和使用

package com.my.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class LoadProp { public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = new Properties();
InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties");
try { prop.load(in);
System.out.println(prop.get("name"));
prop.setProperty("name", "Lucy");
System.out.println(prop.get("name")); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }

4.使用 java.util.Enumeration 获取所有key

package com.my.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties; public class LoadProp { @SuppressWarnings("unchecked")
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = new Properties();
InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties");
Enumeration<String> en = null;
String key = null;
try {
prop.load(in); en = (Enumeration<String>) prop.propertyNames();
while (en.hasMoreElements()) {
key = (String) en.nextElement();
System.out.println(key);
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }

2018-06-08 15:00

2020年5月1日 16:48:08

3.Properties文件的加载和使用的更多相关文章

  1. 【Spring MVC】Properties文件的加载

    [Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...

  2. properties文件的加载方式

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  3. Java读取Properties文件 Java加载配置Properties文件

    static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...

  4. Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载

    转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...

  5. C#遍历XML文件动态加载菜单

    通过遍历XML文件动态加载菜单,顺便利用WebBrowser控件实现一个简单的桌面浏览器 效果如下: 代码如下: XMLFile1.xml <?xml version="1.0&quo ...

  6. log4j.properties配置与加载应用

    log4j.properties总结:   一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器 ...

  7. xib文件的加载方法

    xib文件的加载方法 以UITableViewCell的cell为例 很多时候因为系统的cell无法满足我们的日常需求,我们都会自定义cell 因为cell的界面比较固定,所以通常都会选择用xib来描 ...

  8. 让Lua自己把文件夹下面的所有文件自动加载起来吧

    没有想到我也做了一回标题党.其实这里边说的自动还是有夸大其词的部分.其实只是指定文件夹,然后根据指定文件夹数据,加载目录下边的内容而已. 怎么来进行Lua文件的加载 一般情况下,相关的功能需要给他创建 ...

  9. 速战速决 (5) - PHP: 动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间

    [源码下载] 速战速决 (5) - PHP: 动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间 作者:webabcd 介绍速战速决 之 PHP 动态地创 ...

随机推荐

  1. 工作中常用的linux命令(1)

    1.cd :进入一个目录,例如进入/home/admin目录:cd /home/admin 2.pwd :查看当前所在目录:如图: 3.ls :列出当前目录下的所有文件: 4.ll :列出当前目录下的 ...

  2. PCI设备内存操作函数总结

    1.  ExAllocatePool() 函数说明: ExAllocatePool allocates pool memory of the specified type and returns a ...

  3. org.apache.jasper.JasperException: /pages/column.jsp (line: 8, column: 1) File "pathTags.jsp" not f

    1.错误描述 21-Mar-2015 00:57:40.934 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationCon ...

  4. directX视频播放------手动连接

    IGraphBuilder *pigb = NULL; IMediaControl *pimc = NULL; IMediaEventEx *pimex = NULL; IVideoWindow *p ...

  5. javaWeb之eclipse创建Servlet模板快捷键设置

    没有模板创建Servlet 出现的是除了doPOST 和doGet方法 还有许多方法和一些注释,页面不够清晰 创建模板的步骤是: 1.点击window下的preference 选项 2在 表单框里填写 ...

  6. 芝麻HTTP:Python爬虫实战之爬取百度贴吧帖子

    本篇目标 1.对百度贴吧的任意帖子进行抓取 2.指定是否只抓取楼主发帖内容 3.将抓取到的内容分析并保存到文件 1.URL格式的确定 首先,我们先观察一下百度贴吧的任意一个帖子. 比如:http:// ...

  7. pat1061-1070

    1061 我想吐槽这题的题意不够清楚,不过下次得长记性,对于模糊的题意要大胆猜测,而不是固执己见 #include<iostream> #include<cstdio> #in ...

  8. QOpenGLTexture 两个纹理叠加

    如何做纹理混合? 方法是,定义多个QOpenGLTexture,然后在fragment shader中添加相应的变量,然后把texture bind到对应的uniform变量上. 废话不多说 text ...

  9. C#简单工厂和抽象类的实例

    using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...

  10. Query 插件为什么要return this.each()

    jQuery.fn.test2= function(){ this.css("background","#ff0");//这里面的this为jquery对象,而 ...