歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java讀取src下的配置文件

Java讀取src下的配置文件

日期:2017/3/1 9:48:03   编辑:Linux編程

新建一個普通的Java項目,在src目錄下有一config.propertes文件。配置文件裡面的內容如下(就一句話):

driverURL = jdbc:mysql://127.0.0.1:3306/evan

下面是讀取配置文件的java代碼:

package com.evan;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {

public static void main(String[] args) throws IOException {
InputStream is = ReadProperties.class.getResourceAsStream("/config.properties");

Properties properties = new Properties();

properties.load(is);

is.close();
String driverURL = properties.getProperty("driverURL");

/*
* 輸入結果為:jdbc:mysql://127.0.0.1:3306/evan
*/
System.out.println(driverURL);
}
}

項目的目錄如下:

Copyright © Linux教程網 All Rights Reserved