[java] read properties or xml file.

ITWeb/개발일반 2013. 4. 5. 10:57

http://javarevisited.blogspot.kr/2012/03/how-to-read-properties-file-in-java-xml.html


[properties file]

jdbc.username=test

jdbc.password=unknown

jdbc.driver=com.mysql.jdbc.Driver


[source code]

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;


public class PropertyFileReader {


    public static void main(String args[]) throws FileNotFoundException, IOException {


        //Reading properties file in Java example

        Properties props = new Properties();

        FileInputStream fis = new FileInputStream("c:/jdbc.properties");

      

        //loading properites from properties file

        props.load(fis);


        //reading proeprty

        String username = props.getProperty("jdbc.username");

        String driver = props.getProperty("jdbc.driver");

        System.out.println("jdbc.username: " + username);

        System.out.println("jdbc.driver: " + driver);


    }

}


[xml file]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

   <properties> <entry key="jdbc.username">root</entry>

        <entry key="jdbc.password">mysql</entry>

   </properties>


[source code]

public static void main(String args[]) throws FileNotFoundException, IOException {


        //Reading properties file in Java example

        Properties props = new Properties();

        FileInputStream fis = new FileInputStream("c:/properties.xml");

      

        //loading properites from properties file

        props.loadFromXML(fis);


        //reading proeprty

        String username = props.getProperty("jdbc.username");

        System.out.println("jdbc.username: " + username);

      

}


: