posts - 22, comments - 32, trackbacks - 0, articles - 71
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

首先在web工程src目录下新建一个database.properties 文件
内容如下:

user=root
password=root
databaseType=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull

 这里的内容随自己的合适而变化,这里不多说了;

在新建一个读取.properties文件新类:

package com.junhai.tamsys.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class DatabaseConfigure {
 private Properties property;
 private FileInputStream inputFile;
 private FileOutputStream outputFile;

 public DatabaseConfigure() {
  property = new Properties();
 }

 public DatabaseConfigure(String filePath) {
  property = new Properties();
  try {
   inputFile = new FileInputStream(filePath);
   property.load(inputFile);
   inputFile.close();
  } catch (FileNotFoundException e) {
   System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /*
  * 重载函数,得到key的值 @param key 取得其值的键 @return key的值
  */
 public String getValue(String key) {
  if (property.containsKey(key)) {
   return property.getProperty(key);

  } else
   return "";
 }

 /*
  * 重载函数,得到key的值
  *
  * @param fileName propertys文件的路径+文件名 @param key 取得其值的键 @return key的值
  */
 public String getValue(String fileName, String key) {
  try {
   String value = "";
   inputFile = new FileInputStream(fileName);
   property.load(inputFile);
   inputFile.close();
   if (property.containsKey(key)) {
    value = property.getProperty(key);
    return value;
   } else
    return value;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return "";
  } catch (IOException e) {
   e.printStackTrace();
   return "";
  } catch (Exception ex) {
   ex.printStackTrace();
   return "";
  }
 }

 /*
  * 清除properties文件中所有的key和其值
  */
 public void clear() {
  property.clear();
 }

 /*
  * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value
  * @param key 要存入的键 @param value 要存入的值
  */
 public void setValue(String key, String value) {
  property.setProperty(key, value);
 }

 /*
  * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。 @param fileName 文件路径+文件名称 @param
  * description 对该文件的描述
  */
 public void saveFile(String fileName, String description) {
  try {
   outputFile = new FileOutputStream(fileName);
   property.store(outputFile, description);
   outputFile.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }

 public static void main(String[] args) {
  DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");
  System.out.println(test.getValue("user"));
  System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));
  
 }
}


这样就可以通过key得到相应的value了;
想在这里多说一点是路径问题,java工程和web 工程读取.properties路径是不一样的,我在这里就花了不少时间。
JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");这样读取就可以了:
web工程这样读取:DatabaseConfigure  dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()
                                                .getResource("").getPath()+"database.properties");
 
因为当服务器启动后工程里面东西会编译后加到\WEB-INF\classes这个目录,服务也是从这个目录下读取信息的。所以先取到这个路径,才能正确读取到database.properties这里边的内容。


只有注册用户登录后才能发表评论。


网站导航: