⑴ java 怎麼讀取配置文件
一.讀取xml配置文件
(一)新建一個java bean(HelloBean. java)
java代碼
(二)構造一個配置文件(beanConfig.xml)
xml 代碼
(三)讀取xml文件
1.利用
java代碼
2.利用FileSystemResource讀取
java代碼
二.讀取properties配置文件
這里介紹兩種技術:利用spring讀取properties 文件和利用java.util.Properties讀取
(一)利用spring讀取properties 文件
我們還利用上面的HelloBean. java文件,構造如下beanConfig.properties文件:
properties 代碼
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用於指定類來源。
然後利用org.springframework.beans.factory.support.來讀取屬性文件
java代碼
(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存伺服器ip地址和埠,如:
properties 代碼
ip=192.168.0.1
port=8080
三.讀取位於Jar包之外的properties配置文件
下面僅僅是列出讀取文件的過程,剩下的解析成為properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");
2 num = reader.read(byteStream);
3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);
四.要讀取的配置文件和類文件一起打包到一個Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //獲取當前Jar文件名,並對其解碼,防止出現中文亂碼
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //寫配置文件
。。。
out.close();
⑵ 怎樣使用配置文件設置Java系統屬性
右擊桌面上「計算機」圖標,從彈出的快捷菜單中選擇「屬性」,打開「系統」窗口。
配置Java的系統環境變數
在打開的「系統」窗口中單擊左側「高級系統設置」選項。
配置Java的系統環境變數
在彈出的「系統屬性」對話框中,切換到「高級」選項卡,單擊「環境變數」按鈕。
配置Java的系統環境變數
4
在彈出的「環境變數」對話框中,在「系統變數」欄中選擇「Path」選項,然後單擊「編輯」按鈕。
配置Java的系統環境變數
⑶ java 可以使用動態參數配置persistence.xml嗎
當然可以,要不然怎麼叫動態配置呢
如果想實現動態配置,那麼每次調用的時候都需要重新load一下
InputStream is = new FileInputStream(file);
即每次調用api,獲取參數的時候,都需要重新載入這個文件即可
⑷ java中conf.properties為什麼能實現動態配置
因為在後台有對這個文件進行載入和解析。把conf.properties這個文件裡面的數據全部解析到一個對象里。
然後在其他地方就可以通過這個對象來獲取配置屬性。
等程序寫好後,以後要修改數據,直接在配置文件里修改,程序就不用修改,也不用重新編譯了。
⑸ java熱部署:tomcat運行中,動態修改配置文件(java文件)中的static屬性並生效
<Context path="/tomcatTest" reloadable="true" docBase="E:\workplace\testProject\WebRoot"/>
第一個是容器里的項目path 要加/
第二個參數是你的workplace的路徑,一般是到webroot
寫個context.xml文件,放到項目的META-INF里.context.xml頭部像上面那樣寫就可以
⑹ java應用(非web應用)中log4j.properties/xml動態修改配置文件,無需重啟,就能立即生效,如何實現
DOMConfigurator.config(filename);
適用於xml配置文件
如果是properties配置文件則用
PropertyConfigurator.configure(filename);
⑺ 配置文件在java 中怎麼創建
1.一般在scr下面新建一個屬性文件*.properties,如a.properties
然後在Java程序中讀取或操作這個屬性文件。
代碼實例
屬性文件a.properties如下:
name=root
pass=liu
key=value
讀取a.properties屬性列表,與生成屬性文件b.properties。代碼如下:
1 import java.io.BufferedInputStream;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import java.util.Properties;
7
8 public class PropertyTest {
9 public static void main(String[] args) {
10 Properties prop = new Properties();
11 try{
12 //讀取屬性文件a.properties
13 InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));
14 prop.load(in); ///載入屬性列表
15 Iterator<String> it=prop.stringPropertyNames().iterator();
16 while(it.hasNext()){
17 String key=it.next();
18 System.out.println(key+":"+prop.getProperty(key));
19 }
20 in.close();
21
22 ///保存屬性到b.properties文件
23 FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打開
24 prop.setProperty("phone", "10086");
25 prop.store(oFile, "The New properties file");
26 oFile.close();
27 }
28 catch(Exception e){
29 System.out.println(e);
30 }
31 }
32 }
getProperty/setProperty這兩個方法是分別是獲取和設置屬性信息。
Properties類繼承自Hashtable類並且實現了Map介面,也是使用一種鍵值對的形式來保存屬性集。不過Properties有特殊的地方,就是它的鍵和值都是字元串類型。
*.properties文件的注釋用#。
配置數據的時候是以鍵值對的形式,調用的時候和修改的時候也是操作鍵值對。
2.當然還可以用*.xml來配置,位置一般在一個包下面。
例如com.styspace包下面的config.properties文件。
xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
</Accounts>
現在操作config.properties文件。
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class peropertiesLoaderTest {
public static void main(String[] args) throws ConfigurationException{
Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
String name = config.getString("name");
System.out.println("name:" + name);
}
}
⑻ Java 配置文件.
不一定,反射可以直接修改屬性,為屬性寫get set方法是因為這是javabean寫法,是一種規范,很多框架都是用這個規范來修改和獲取對象屬性的,所以寫這兩個方法只是為了能使用這些規范開發出來的框架
⑼ spring如何動態載入配置文件,就是配置文件修改了,application.xml如何能讀取到
項目,需要訪問多個資料庫,而且需要在伺服器運行不重新啟動的情況下,動態的修改spring中配置的數據源datasource,在網上找了很多資料,最後找到了適合我的方法,下面總結一下。
spring的配置文件是在容器啟動的時候就載入到內存中的,如果手動改了application.xml,我們必須要重新啟動伺服器配置文件才會生效。而在spring中提供了一個類WebApplicationContext,這個類可以讓你獲得一些bean,可以修改內存中的信息,我就是通過這個類來實現的。下面是我具體的代碼。
package com.southdigital.hospital;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ChangeSpringConfig extends HttpServlet
{
private String ipAddress = "127.0.0.1";
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//先取得servleContext對象,提供給spring的WebApplicationUtils來動態修改applicationContext.xml
ipAddress = request.getParameter("ipAddress");
System.out.println(ipAddress);
ServletContext servletContext = this.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource");
cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh");
}
}
注意:通過這種方法修改applicationContext.xml文件的時候用c3p0,而不可以用dbcp,dbcp不支持動態修改讀取到內存裡面的數據。
spring 3.1已經支持了。
⑽ 如何為java 程序 寫配置文件
假設有如下xml配置文件config.xml:
?xml
version="1.0"
encoding="utf-8"
?
kiyho
sinkiang
100
可以用以下代碼訪問:
import
org.apache.commons.configuration.configurationexception;
import
org.apache.commons.configuration.xmlconfiguration;
public
class
xmlconfigdemo
{
public
static
void
main(string[]
args)
{
try
{
xmlconfiguration
config
=
new
xmlconfiguration("config.xml");
system.out.println(config.getlist("name"));
system.out.println(config.getint("info.age"));
}
catch
(configurationexception
e)
{
e.printstacktrace();
}
}
}