導航:首頁 > 文件教程 > java通用讀取配置文件

java通用讀取配置文件

發布時間:2023-03-03 15:39:32

java讀取文件的幾種方式

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

linux 下java讀取配置文件

linux下也是文件系統,同樣的也可以使用file對象來讀取配置文件信息,示例如下:
import java.io.*;

public class FileToString {
public static String readFile(String fileName) {
String output = "";

File file = new File(fileName);//建立file對象

if(file.exists()){//判斷是否存在
if(file.isFile()){//判斷是否文件
try{
BufferedReader input = new BufferedReader (new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;

while((text = input.readLine()) != null)
buffer.append(text +"/n");//讀取內容進行拼接。

output = buffer.toString();
}
catch(IOException ioException){
System.err.println("File Error!");

}
}
else if(file.isDirectory()){//是否為文件夾
String[] dir = file.list();
output += "Directory contents:/n";

for(int i=0; i<dir.length; i++){
output += dir[i] +"/n";
}
}
}
else{
System.err.println("Does not exist!");
}
return output;
}
}

Ⅲ java 怎麼讀取web jar中的某個配置文件

項目遷移的過程中發現以前的代碼維護性實在是差。
我把問題簡化為以下這專些簡單的代碼:屬
項目M
引用了項目
A.jar,這個A在lib目錄裡面
在A裡面放置了一個配置文件test.properties,
就放在jar的根目錄下。
A.jar
|___test.properties
在M中有一段代碼回去讀取這個A.jar里的配置文件,簡單一點就用下面這句話來調用。
Java
code
public
class
ConfigUtil
{
public
static
String
getInstance()
throws
Exception{
String
path
=
ConfigUtil.class.getResource("/").toString();
path
=
path.substring(0,
path.length()-8);//
System.out.println(path);//這里列印的結果顯示可以拿到當前類的絕對路徑
InputStream
f
=
new
FileInputStream("jar:"+path+"lib!/A.jar/"+"test.properties");
return
"xxx";
}
}

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

/*
* IniReader.java
* 用Java讀取INI文件(帶section的)
* 示例:
* IniReader reader = new IniReader("E:\\james\\win.ini");
* out.println(reader.getValue("TestSect3", "kkk 6"));
*/

package tmp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class IniReader {

protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;

public IniReader(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}

protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}

protected void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currentSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name, value);
}
}
}

public String getValue(String section, String name) {
Properties p = (Properties) sections.get(section);

if (p == null) {
return null;
}

String value = p.getProperty(name);
return value;
}

}

ini文件如下:

[TestSect1]
kkk 1=VVVVVVVVVVV1
kkk 2=VVVVVVVVVVV2

[TestSect2]
kkk 3=VVVVVVVVVVV3
kkk 4=VVVVVVVVVVV4

[TestSect3]
kkk 5=VVVVVVVVVVV5
kkk 6=VVVVVVVVVVV6

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

最常用讀取properties文件的方法 InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用: InputStream ins = this.getClass().getResourceAsStream(

Ⅵ java讀取配置文件的方法(xml)

#include <XMLDoc.hpp>
——相關說明
_di_IXMLDocument 為模板類
typedef System::DelphiInterface< IXMLDocument > _di_IXMLDocument;

_di_IXMLNode
typedef System::DelphiInterface< IXMLNode > _di_IXMLNode;

_di_IXMLNodeList 同

——類方法
//設置參數
void TXXX::setOptions(String name,String value){

//創建文檔對象
_di_IXMLDocument XMLDoc = LoadXMLDocument(L"文件路徑");

XMLDoc->Active=true;

//文檔根節點
_di_IXMLNode root = XMLDoc->DocumentElement;

//想要查找節點
_di_IXMLNode tempNode;

//調用搜索方法
searchXml(name,root,tempNode);

// 處理
if(tempNode!=NULL)
tempNode->SetText(value);
XMLDoc->SaveToFile(L"文件路徑");
}
//遞歸搜索參數節點樹
void TXXX::searchXml(String name,_di_IXMLNode &Parent_Node,_di_IXMLNode& tempNode){

_di_IXMLNode Child_Node; //子結點
//子節點列表
_di_IXMLNodeList list = Parent_Node->ChildNodes;
for(int i=0;i<list->Count;i++)
{
Child_Node = list->Get(i);
//遞歸結束條件
if(Child_Node->GetNodeName()==name)
{
tempNode = Child_Node;
break;
}
else
{
//遞歸函數
searchXml(name,Child_Node,tempNode);
}
}
}

Ⅶ java web工程,讀取配置文件路徑問題

讀取配置文件 , xxx.properties放在/WEB-INF/classes/目錄下

首先將配置文件轉換成InputStream,有兩種方式,原理一樣,都是通過類載入器得到資源:

(1)InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("xx.properties");
(2) InputStream inputStream =
this.getClass() .getClassLoader().getResourceAsStream( "xx.properties" );
調用對象的getClass()方法是獲得對象當前的類類型,這部分數據存在方法區中,
而後在類類型上調用 getClassLoader()方法是得到當前類型的類載入器,我們知道在Java中所有的類都是通過載入器載入到虛擬機中的,而且類載入器之間存在父 子關系,就是子知道父,父不知道子,這樣不同的子載入的類型之間是無法訪問的(雖然它們都被放在方法區中),所以在這里通過當前類的載入器來載入資源也就 是保證是和類類型同一個載入器載入的。
最後調用了類載入器的getResourceAsStream()方法來載入資源。

(3) 然後載入配置文件,讀取屬性值
Properties prop = new Properties();
prop.load(input);
String value = prop.getProperty("PropertyName");

input.close();

Ⅷ java怎樣提取配置文件!怎麼才能採用ServletContext讀取

創建配置文件:
1、在項目的任意地方,右鍵-》New-》File-》FileName-》輸入-》名稱.properties(比如:config.properties)
2、訪問路徑:從根目錄開始出發(WebRoot)->WEB-INF->classes->config.properties,(如果有包名,在classes->包名->config.properties)(路徑可以直接從本地中項目的路徑,找到WEB-INF直接從地址中(比如我的本地磁碟保存是這樣的:F:\課程\s2課程\s2書上內容\Java Web\ServletTest\WebRoot\WEB-INF\classes\config.properties))

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
/************************使用servletContext.getResourceAsStream**************************************/
//實例化ServletContext
ServletContext servletContext=this.getServletContext();
// //獲取輸入流
// InputStream in=servletContext.getResourceAsStream("\\WEB-INF\\classes\\config.properties");
// Properties p=new Properties();
// //類的裝載
// p.load(in);
// //拿到配置文件中userName參數
// out.println(p.getProperty("userName"));

/***************************普通的獲取配置文件**************************************/
String path= servletContext.getRealPath(("\\WEB-INF\\classes\\config.properties"));//拿到絕對路徑
FileInputStream in=new FileInputStream(path);
Properties p=new Properties();
p.load(in);
out.println(p.get("userName"));

Ⅸ Java讀取xml配置文件:

你用DOM把賬號節點取出來
然後用String中的split方法做切割
就可以放到數組里了
如果單純是想看賬號是不是在這個裡面
你也可以直接String.indexof查找讀出來的信息中有沒有你要的賬號

閱讀全文

與java通用讀取配置文件相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接