一、properties类读取配置文件

1、从项目中读取properties配置文件,新建一个main程序,对应位置建立一个名称为config.properties配置文件,随意放置一些键值对。IDEA建立配置文件,对应package鼠标右键,点击New,再点击Resource Bundle,弹出对话框输入名称保存即可。

2、以下实例,读取配置文件,然后遍历key值和value值,采用此种方式读取项目中配置文件,将不依赖具体环境。

 1 public class HelloWorld {
2 public static void main(String[] args) {
3 Properties properties=new Properties();
4 InputStream in=HelloWorld.class.getClassLoader().getResourceAsStream("Config/config.properties");
5 try{
6 properties.load(in);
7 Enumeration enumeration=properties.propertyNames();
8 while (enumeration.hasMoreElements()){
9 String strKey=(String)enumeration.nextElement();
10 String strValue=properties.getProperty(strKey);
11 System.out.println("key:"+strKey+";Value:"+strValue);
12 }
13 }catch(Exception e){
14 System.out.println("There is An IO error!");
15 }
16 }
17 }

  (1)读取后另一种遍历方式

1     InputStream in = new BufferedInputStream(new FileInputStream("D:/b.properties"));
2 properties.load(in);
3 Iterator<String> it = properties.stringPropertyNames().iterator();
4 while (it.hasNext()) {
5 String key = it.next();
6 System.out.println(key + ":" + properties.getProperty(key));
7 }

3、一些其它读取配置文件的方法

  (1)从环境固定位置读取配置文件,配置文件放在某个磁盘下几种方式。   

1    FileInputStream in=new FileInputStream("D:/config.properties");
2 properties.load(in);

  或

1    InputStream in= new BufferedInputStream(newFileInputStream("D:/config.properties"));
2 properties.load(in);

  或

1    BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/config.properties"));
2 properties.load(bufferedReader);

4、配置文件添加新的键值对,然后将保存成一个新文件并存储。

1    FileOutputStream oFile = new FileOutputStream("D:/b.properties", true);//true表示追加打开
2 properties.setProperty("移动", "10086");
3 properties.store(oFile, "The New properties file has Created");
4 oFile.close();

二、java.util.ResourceBundle类读取properties配置文件,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

1    ResourceBundle resource = ResourceBundle.getBundle("Config/config");
2 String key = resource.getString("foo");
3 System.out.println(key);

三、XML配置文件读取

使用dom4j解析xml,下载地址:https://dom4j.github.io/

1、xml文件内容

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <CONFIG>
3 <VALUE>
4 <!-- mysql连接设置 -->
5 <server>127.0.0.1</server>
6 <dbname>users</dbname>
7 <user>root</user>
8 <pass>pass</pass>
9 <port>3306</port>
10 </VALUE>
11 </CONFIG>

2、XML项目中所在位置

3、测试代码

 1 import org.dom4j.Document;
2 import org.dom4j.Element;
3 import org.dom4j.io.SAXReader;
4 import java.io.File;
5 import java.net.URL;
6 import java.util.Iterator;
7
8 try {
9 URL filePath=null;
10 filePath = HelloWorld.class.getClassLoader().getResource("config/aaa.xml");
11 File f =new File(filePath.getPath());
12 if (!f.exists()) {
13 System.out.println(" Error : Config file doesn't exist!");
14 System.exit(1);
15 }
16 SAXReader reader = new SAXReader();
17 Document doc;
18 doc = reader.read(f);
19 Element root = doc.getRootElement();
20 Element data;
21 Iterator<?> itr = root.elementIterator("VALUE");
22 data = (Element) itr.next();
23 String server = data.elementText("server").trim();
24 String user = data.elementText("user").trim();
25 String pass = data.elementText("pass").trim();
26 String port = data.elementText("port").trim();
27 String dbname = data.elementText("dbname").trim();
28 System.out.println(server);
29 System.out.println(user);
30 System.out.println(pass);
31 System.out.println(port);
32 System.out.println(dbname);
33 } catch (Exception ex) {
34 System.out.println("Error : " + ex.toString());
35 }

4、注意事项:以上的获取XML配置资源,主要依赖第三方包dom4j,下载地址在上面已经给出。将第三方jar包引用到当前项目可以参考:https://blog.csdn.net/MAOZEXIJR/article/details/88966876

四、尊重原创,以上有些内容参考了以下链接内容。

https://www.cnblogs.com/mengxiangqihang/p/4150450.html

https://www.cnblogs.com/xudong-bupt/p/3758136.html

https://www.cnblogs.com/bakari/p/3562244.html

      https://www.cnblogs.com/xudong-bupt/p/3758136.html

Java Properties配置文件和XML配置文件读取的更多相关文章

  1. Java学习-023-Properties 类 XML 配置文件读取及写入源代码

    之前的几篇 Properties 文章已经讲述过了 Java 配置文件类 Properties 的基本用法,查看 JDK 的帮助文档时,也可看到在 Properties 类中还有两个方法 loadFr ...

  2. Spring框架 全注解annotation不使用配置文件(SpringConfiguration.java类代替) 补充 xml配置文件没有提示解决

    全注解不使用配置文件 首先还是倒包 在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.b ...

  3. 基于Java Properties类设置本地配置文件

    一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...

  4. Java web 项目 web.xml 配置文件加载过程

    转载自:http://blog.csdn.net/luoliehe/article/details/46884757#comments WEB加载web.xml初始化过程: 在启动Web项目时,容器( ...

  5. ssm框架 spring的主配置文件 spring-mvc主配置文件 web.xml配置文件(基础的配置文件)

    1.spring主配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  6. Spring配置文件和SpringMVC配置文件 web.xml配置文件 保存自用

    话不多说,最近在周末自己抽时间写一些框架做的系统,当所有东西都需要自己配置时候发现自己压根记不住这么多类和路径,所以日常总结就变得尤为重要了 db-config.properties 将配置文件常量提 ...

  7. zookeeper中controller项目中资源配置文件applicationContext.xml配置文件编写

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. WEB.xml配置文件解读

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  9. javaweb项目中关于配置文件web.xml的解析

    一..启动tomcat,加载项目中的web.xml文件,创建servercontext上下文对象. 可以通过servercontext对象在应用中获取web.xml文件中的值. web应用加载的顺序与 ...

  10. Java Web的web.xml文件作用及基本配置(转)

    其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...

随机推荐

  1. Server check fail, please check server xxx ,port 9848 is available

    [1]如果使用docker安装的nacos服务,2.x版本后增加了 grpc 通信并且增加nacos的集群端口上下偏移1000,创建容器时除了8848还需要把7848.9848都暴露出来.如:-p 7 ...

  2. [转]CMake:相关概念与使用入门

    CMake:相关概念与使用入门(一) CMake:搜索文件和指定头文件目录(三) CMake 子工程添加 根目录中他文件夹里的cpp文件 翻译 搜索 复制

  3. [转]spring-framework-x.x.x.RELEASE-dist下载教程

    原文链接: spring-framework-x.x.x.RELEASE-dist下载教程

  4. ant利用ivy从maven仓库下载项目所依赖的jar包默认的存储位置

    ant利用ivy从maven仓库下载项目所依赖的jar包默认的存储位置为: 当前登录系统的用户目录下(如"C:\Users\Administrator\.ivy2\cache"), ...

  5. 移动端IM开发者必读(三):爱奇艺移动端跨国弱网通信的优化实践

    本文由爱奇艺技术团队分享,作者isno,原题"爱奇艺海外App的网络优化实践",下文进行了排版和内容优化等. 1.引言 做海外市场,特别目标是面向全球的用户,网络的重要性不言而喻. ...

  6. IM跨平台技术学习(九):全面解密新QQ桌面版的Electron内存优化实践

    本文由QQ技术团队分享,本文收录时有内容修订和大量排版优化. 1.引言 QQ 作为国民级应用,从互联网兴起就一直陪伴着大家,是很多用户刚接触互联网就开始使用的应用. 而 QQ 桌面版最近一次技术架构升 ...

  7. document.querySelector 有多个类 的情况

    document.querySelector 有多个类 document.querySelector 方法用于返回文档中匹配指定 CSS 选择器的第一个元素.如果要查询具有多个类的元素,可以将它们作为 ...

  8. spring-springMVC-总结列表

    Spring 的优良特性 非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API 控制反转:IOC--Inversion of Control,指的是将对象的创建权交给 Spri ...

  9. 网络通信协议:TCP(三次握手四次挥手)和UDP

    通信要素2:网络协议  网络通信协议计算机网络中实现通信必须有一些约定,即通信协议,对速率.传输代码.代码结构.传输控制步骤.出错控制等制定标准. 问题:网络协议太复杂计算机网络通信涉及内容很多, ...

  10. 补充:字符编码ASCII、 ISO8859-1、GB2312、GBK、Unicode、UTF-8

    补充:字符编码  编码表的由来计算机只能识别二进制数据,早期由来是电信号.为了方便应用计算机,让它可以识别各个国家的文字.就将各个国家的文字用数字来表示,并一一对应,形成一张表.这就是编码表. 常 ...