當前位置:首頁 » 數據倉庫 » java如何讀寫配置文件
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java如何讀寫配置文件

發布時間: 2022-01-11 23:09:59

A. Java中如何設置讀取ini配置文件

//讀取一般的屬性文件
FileInputStreamfin=newFileInputStream("my.ini");//打開文件
Propertiesprops=newProperties();//建立屬性類
props.load(fin);//讀入文件
fin.close();//關閉文件

B. 怎樣讀取和寫我自己寫的一個java配置文件

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @Title: JavaBeanCreater.java
* @Package liaodong.xiaoxiao.dom4j.sxd.bean
* @Description: TODO(根據xml配置文件,生成javabean)
* @author 遼東小小
* @date 2012-3-1 上午09:13:25
* @version V1.0
*/
public class JavaBeanCreater
{
public static void main(String[] args)
{
//xml的存放路徑
String xmlPath = "D:\\test\\javabean.xml";
File xmlFile = new File(xmlPath);

StringBuffer sBuffer = new StringBuffer("public class ");

SAXReader sReader = new SAXReader();
Document document;
try
{
//讀取xml文件,返回document對象
document = sReader.read(xmlFile);
//獲取根元素
Element rootElement = document.getRootElement();
//
String className = rootElement.elementText("classname");
sBuffer.append(className).append("\n");
sBuffer.append("{\n");
String nature = rootElement.elementText("nature");
String natures[] = nature.split(",");
//構造成員變數
for (String n : natures)
{
sBuffer.append("private String ").append(n).append(";\n");
}
//構造set/get方法
for (String n : natures)
{
String methodName = n.substring(0, 1).toUpperCase()+n.substring(1);
//拼寫set方法

sBuffer.append("public void set").append(methodName).append("( String ").append(n).append(" )\n{");
sBuffer.append("this.").append(n).append(" = ").append(n).append(";\n}\n");
//拼寫get方法
sBuffer.append("public String get").append(methodName).append(" ( )\n{\n");
sBuffer.append("return ").append(n).append(";\n}\n");
}

//language 方法跟上面的類似 省略。。。。。。。。

//寫文件的方法也省略了。。。。。。。。。
sBuffer.append("}");
System.out.println(sBuffer.toString());
//

}catch(Exception e){
e.printStackTrace();
}
}
}
用Dom4j解析的xml
##############################################################
運行結果為:
public class animal
{
private String name;
private String food;
private String age;
public void setName( String name )
{this.name = name;
}
public String getName ( )
{
return name;
}
public void setFood( String food )
{this.food = food;
}
public String getFood ( )
{
return food;
}
public void setAge( String age )
{this.age = age;
}
public String getAge ( )
{
return age;
}
}

C. Java讀寫文件的幾種方法

java讀取配置文件的幾種方法如下:
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。

D. java如何讀取XML配置文件

學一下jdom吧,很簡單的,可以很方便的提取xml的值,網上很多相關教程(推薦張龍的xml培訓視頻)

E. java中如何從當前項目中讀取運行另一項目中的配置文件

使用url統一資源定位符,前提是需要有訪問文件的許可權。


importjava.io.File;
importjava.net.URL;

importorg.apache.commons.io.FileUtils;

publicclassDownloadURLFile{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){

Stringres=downloadFromUrl("http://images.17173.com/2010/www/roll/201003/0301sohu01.jpg","d:/");
System.out.println(res);
}


(Stringurl,Stringdir){

try{
URLhttpurl=newURL(url);
StringfileName=getFileNameFromUrl(url);
System.out.println(fileName);
Filef=newFile(dir+fileName);
FileUtils.URLToFile(httpurl,f);
}catch(Exceptione){
e.printStackTrace();
return"Fault!";
}
return"Successful!";
}

(Stringurl){
Stringname=newLong(System.currentTimeMillis()).toString()+".X";
intindex=url.lastIndexOf("/");
if(index>0){
name=url.substring(index+1);
if(name.trim().length()>0){
returnname;
}
}
returnname;
}
}

F. JAVA 讀寫配置文件時

這個跟工具有關,你用的事eclipse吧,它對後綴為.properties的文件自動的轉碼中文,變成\啥啥啥的,但是注釋我的可以直接顯示中午啊,而且對於那些看不到的\xxx滑鼠移動上去等一會也可以看到中文

G. java 怎麼讀取配置文件代碼

方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。
因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
具體舉例如下:
//ServletContext.getRealPath(name)讀取路徑
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throwsServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //讀取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServlet

H. 如何在java類中讀取Properties配置文件

Filefile=newFile("properties文件路徑");
if(file.exists()){
InputStreaminputstream=newFileInputStream(file);
Propertiesproperties=newProperties();
properties.load(inputstream);
properties.getProperty("key");
}

這樣就能取到了

I. JAVa讀取配置文件位置ReaderProperties類要怎麼寫

package com;

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

/**
* <p>讀取配置文件properties文件</p>
*/
public class Configuration {
private Properties pro;
private FileInputStream inputFile;
private FileOutputStream outputFile;

public Configuration(){
pro = new Properties();
}

/**
* <p>初始化Configuration類</p>
* @param filePath 要讀取的配置文件的路徑+名稱
*/
public Configuration(String filePath){
pro = new Properties();
try {
//讀取屬性文件
inputFile = new FileInputStream(filePath);
//裝載文件
pro.load(inputFile);
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("裝載文件--->失敗!");
e.printStackTrace();
}
}

/**
* <p>得到key值</p>
* @param key 取得其值的鍵
* @return 取得鍵值
*/
public String getValue(String key){
if(pro.containsKey(key)){
String value = pro.getProperty(key);
return value;
}else{
return "";
}
}

/**
* <p>得到key值</p>
* @param filePath properties文件的路徑+文件名
* @param key 取得其值的鍵
* @return 取得鍵值
*/
public String getValue(String filePath, String key){
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

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

/**
* <p>改變或添加一個key的值</p>
* 當key存在於properties文件中時該key的值被value所代替,
* 當key不存在時,該key的值是value
* @param key 要存入的鍵
* @param value 要存入的值
*/
public void setValue(String key, String value){
pro.setProperty(key, value);
}

/**
* <p>將更改後的文件數據存入指定的文件中,該文件可以事先不存在</p>
* @param fileName 文件路徑+文件名稱
* @param description 對該文件的描述
*/
public void saveFile(String fileName, String description){
try {
outputFile = new FileOutputStream(fileName);
pro.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}

/**
* 測試方法
* @param args
*/
public static void main(String[] args) {
Configuration conf = new Configuration(".\\.\\WebContent\\WEB-INF\\jdbc.properties");
String value = conf.getValue("hibernate.dialect");
System.out.println(value);
}
}

J. 如何讀寫ini配置文件 java

//讀取一般的屬性文件FileInputStreamfin=newFileInputStream("my.ini");//打開文件Propertiesprops=newProperties();//建立屬性類props.load(fin);//讀入文件fin.close();