加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【Java Web】——配置文件的格式之xml和properties的区分

发布时间:2020-12-16 08:29:41 所属栏目:百科 来源:网络整理
导读:【何为配置文件】 百度百科解释:用户配置文件就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置等

【何为配置文件】


百度百科解释:用户配置文件就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置等。

而在我们的项目中的配置文件一般存储的是项目启动所需的环境设置,例如数据库驱动,用户名以及密码等。而在我们做项目的时候,配置文件一般为后缀名是.xml或者是.properties的文件,当然两者的共同点就是存储项目启动所需的环境设置啦!今天我们主要对比一下他们之间的不同。

【看外表】


1、sys-config.xml




2、sys-config.properties




【对比】从表面上看,.xml格式的要比.properties格式的结构要清晰一些,而.properties文件要比.xml文件结构要简单一些。

【看内涵】


1、从结构上来说:

.xml文件主要是树形结构。
.properties文件主要是以key-value键值对的形式存在。

2、从灵活程度上来说:

.xml格式的文件要比.properties格式的文件更灵活一些
.properties格式的文件已键值对形式存在,主要就是赋值,而且只能赋值,不能够进行其他的操作。
.xml格式的文件可以有多种操作方法,例如添加一个属性,或者做一些其他的定义等。
3、从使用便捷程度来说:
.properties格式的文件要比.xml文件配置起来简单一些。
配置.properties只需要简单的getProperty(key)方法或者setProperty(key,value)方法就可以读取或者写入内容;
配置.xml文件的时候通常要查看文档,因为配置比较繁琐,花费时间长才可以配置完整。


4、从应用程度上来说:

.properties文件比较适合于小型简单的项目。
.xml文件适合大型复杂的项目,因为它比较灵活。
【代码实现:配置文件的写入与读取】
1、读写sys-config.properties文件

package testXMLDemo;

import java.io.*import java.util.Iteratorimport java.util.Properties
/**  * 读写Properties属性文件  * Created by Cindy on 2016/9/16.  */ public class propertiesDemo {

    public static void main(String [] args) throws IOException{
        //readProperties();
        writeProperties()    }

    读取properties*/  readProperties()throws IOException{
        Properties prop=new Properties()        FileInputStream inputFile=new FileInputStream("src/sys-config.properties")        prop.load(inputFile)        Iterator<String> it=prop.stringPropertyNames().iterator()        while(it.hasNext()){
            String key=it.next()            System.out.println(key+":"+prop.getProperty(key))        }
        inputFile.close()生成writeProperties()  {

        Properties prop=        try{
            //FileOutputStream oFile=new FileOutputStream("sys-config.properties",true);
            FileOutputStream oFile=new FileOutputStream(new File(,true)            prop.setProperty("driver-name""oracle.jdbc.driver.OracleDriver")"url""jdbcoracle:thin:@localhost:1521:ORCL")"user-name""drp1")"password"            prop.store(oFile"sys-config")            oFile.close()        } catch (FileNotFoundException e) {
            System.out.println(e)catch (IOException e) {
            System.        }
    }
}


2、读写sys-config.xml文件(以dom4j方法为例)

import org.dom4j.*import org.dom4j.io.SAXReaderimport org.dom4j.io.XMLWriterimport java.io.Fileimport java.io.FileWriterimport java.io.IOExceptionimport java.util.List * DOM4J XML * Created by Cindy on 2016/9/16. public class dom4JDemo { XML文件 public static void generateDocument(){ Document document = DocumentHelper.createDocument(); //创建文档对象 Element catalogElement=document.addElement("catalog"); //创建元素 Element journalElement=catalogElement.addElement("journal") Element articleElement=journalElement.addElement("article") Element titleElement=articleElement.addElement("title") titleElement.setText("Java Configuration with XML Schema"); //设置元素内容值 Element authorElement=articleElement.addElement("author") Element firstElement=authorElement.addElement("first") firstElement.addText("Han") Element lastElement=authorElement.addElement("last") lastElement.addText("Cindy") try{ XMLWriter output =new XMLWriter(new FileWriter("D:/catalog.xml"))); //创建输出对象 output.write(document); //将文档对象写入输出对象中 output.close()catch (IOException e) { System.out.println(e.getMessage()) } } * @throws DocumentException */ getXML() throws DocumentException { SAXReader reader=new SAXReader() Document document=reader.read("src/sys-config.xml")); //获取文档对象 Element node=document.getRootElement(); //获得根节点 Element dbinfoElement=node.element("db-info"); //获得元素节点 Element drivernameElement=dbinfoElement.element("driver-name") Element urlElement=dbinfoElement.element("url") Element usernameElement=dbinfoElement.element("user-name") Element passwordElement=dbinfoElement.element("password") String drivername=drivernameElement.getText(); //获得节点内容 String url=urlElement.getText() String username=usernameElement.getText() String password=passwordElement.getText() System.out.println(drivername)out.println(url)out.println(username)out.println(password); } throws DocumentException { //generateDocument(); getXML() } }

通过代码我们可以看出.properties文件的读取写入还是比较简单的,而.xml文件的读取和写入就相对于复杂一些,不过它也挺容易理解的。
【总结】

在敲网上商城的时候好像用到过.properties文件,那时候还很模糊,现在在做DRP,配置文件用的是.xml格式的,而公司项目中的配置文件是用的.properties格式的,就想着对比一下它们之间的不同。其实,配置文件还有其他格式的,例如.ini格式的文件,或者".cfg",".conf",".txt"等格式的文件都可以作为配置文件,只不过我接触到的常见的是.xml和.properties文件。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读