一、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. 2024年1月Java项目开发指南3:创建Springboot项目

    本文档编写于贰零贰肆年一月八日@萌狼蓝天 如果你不知道什么是springboot,那么你只需要知道,这是一个让我们减少配置工作量,方便开发的开发框架,能让我们更专心于业务开发,省的被各种各样的配置浪费 ...

  2. 【C#】【报错解决】分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。

    服务器:Windows Server 数据中心 2016 问题描述: 在本地测试正常运行,但是上传到服务器却出现该错误 报错: 分析器错误消息: 无法识别的属性"targetFramewor ...

  3. Qt音视频开发30-Onvif事件订阅

    一.前言 能够接收摄像机的报警事件,比如几乎所有的摄像机后面会增加报警输入输出接口,如果用户外接了报警输入,则当触发报警以后,对应的事件也会通过onvif传出去,这样就相当于兼容了所有onvif摄像机 ...

  4. IM跨平台技术学习(十一):环信基于Electron打包Web IM桌面端的技术实践

    本文由环信技术黄飞鹏分享,原题"实战|如何利用 Electron 快速开发一个桌面端应用",本文进行了排版和内容优化等. 1.引言 早就听说利用Electron可以非常便捷的将网页 ...

  5. vue3-openlayers基础知识简介

    vue3-openlayers基础知识简介 OpenLayers 3 Primer openlayers6:入门基础(一) openlayers 入门教程 一.基础概念介绍 地图(Map) OpenL ...

  6. 冷水机超频AMD 2600X 4.5G

    整个测试大概持续了一个半小时,期间水温保持在2度左右(±1摄氏度).后来开始冷凝,而且超频也到了4.5G瓶颈,所以停了下来. 全核4.4G过R154.5G过CPU-Z认证(点击查看)温度表现,一般吧. ...

  7. IDEA集成Docker控制台日志乱码问题解决

    IDEA工具栏 → HELP → Edit Custom VM Options... 文件尾部添加一行 -Dfile.encoding=utf-8 重启IDEA即可

  8. Solution Set - “伸手向着拉格朗日点作别”

    目录 0.「UR #9」「UOJ #133」电路手动分析 1.「UR #9」「UOJ #134」App 管理器 2.「UR #10」「UOJ #152」汉诺塔 3.「UNR #2」「UOJ #312」 ...

  9. Solution Set -“似一捧细泉的奔逃”

    目录 0.「OurOJ #47912」优美的分配方案 1.「OurOJ #47927」海之女仆 2.「OurOJ #47950」中档题 3.「OurOJ #47933」坐标 4.「OurOJ #479 ...

  10. Linux:yum

    yum介绍 [yellow dog updater,modified],一个在Fedora和RedHat以及SUSE.Centos中的shell前段软件包管理器 能够自动的从指定的服务器自动下载RPM ...