`

读取properties

    博客分类:
  • java
 
阅读更多
package com.my.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class TestProperties {

public static void main(String[] args) {
/*
    jdbc.properties内容如下:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8
username=root
password=my
maxActive=500
maxIdle=10
minIdle=1
initialSize=1
*/
readProperties(new File("jdbc.properties")); 
   
}

private static void readProperties(File file) {
InputStream inputStream=null;
Properties p=null;
try {
inputStream = new FileInputStream(file);
        p = new Properties(); 
        p.load(inputStream); 
        inputStream.close(); 
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(
"driverClassName:"+p.getProperty("driverClassName")+"\n"
+"url:"+p.getProperty("url")+"\n"
+"username:"+p.getProperty("username")+"\n"
+"password:"+p.getProperty("password")+"\n"
+"maxActive:"+p.getProperty("maxActive")+"\n"
+"maxIdle:"+p.getProperty("maxIdle")+"\n"
+"minIdle:"+p.getProperty("minIdle")+"\n"
+"initialSize:"+p.getProperty("initialSize"));
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics