㈠ 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讀取配置文件的幾種方法如下:
方式一:採用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讀取配置文件
如果是相對路徑無論windows還是linux都是一樣的。
如果是絕對路徑,只需要看好配置文件所在linux的目錄,其他的操作和windows一樣(例如:linux【/usr/local/src/1.config】,windows【c:/1.config】)
使用File就可以直接讀取了。
㈣ java 中配置文件路徑讀取使用FileReader和InputStream區別和用法
一、按數據來源(去向)分類:
1、是文件: FileInputStream, FileOutputStream, FileReader, FileWriter
2、是byte[]:ByteArrayInputStream, ByteArrayOutputStream
3、是Char[]: CharArrayReader, CharArrayWriter
4、是String: StringBufferInputStream, StringReader, StringWriter
5、網路數據流:InputStream, OutputStream, Reader, Writer
二、按是否格式化輸出分:
1、要格式化輸出:PrintStream, PrintWriter
三、按是否要緩沖分:
1、要緩沖:BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
四、按數據格式分:
1、二進制格式(只要不能確定是純文本的): InputStream, OutputStream及其所有帶Stream結束的子類
2、純文本格式(含純英文與漢字或其他編碼方式);Reader, Writer及其所有帶Reader, Writer的子類
五、按輸入輸出分:
1、輸入:Reader, InputStream類型的子類
2、輸出:Writer, OutputStream類型的子類
六、特殊需要:
1、從Stream到Reader,Writer的轉換類:InputStreamReader, OutputStreamWriter
2、對象輸入輸出:ObjectInputStream, ObjectOutputStream
3、進程間通信:PipeInputStream, PipeOutputStream, PipeReader, PipeWriter
4、合並輸入:SequenceInputStream
5、更特殊的需要:PushbackInputStream, PushbackReader, LineNumberInputStream, LineNumberReader
決定使用哪個類以及它的構造進程的一般准則如下(不考慮特殊需要):
首先,考慮最原始的數據格式是什麼: 原則四
第二,是輸入還是輸出:原則五
第三,是否需要轉換流:原則六第1點
第四,數據來源(去向)是什麼:原則一
第五,是否要緩沖:原則三 (特別註明:一定要注意的是readLine()是否有定義,有什麼比read, write更特殊的輸入或輸出方法)
第六,是否要格式化輸出:原則二
Java中Inputstream與Reader的區別
Reader支持16位的Unicode字元輸出,InputStream支持8位的字元輸出。
Reader和InputStream分別是I/O庫提供的兩套平行獨立的等級機構,
InputStream、OutputStream是用來處理8位元的流,
Reader、Writer是用來處理16位元的流。
而在JAVA語言中,byte類型是8位的,char類型是16位的,所以在處理中文的時候需要用Reader和Writer。
值得說明的是,在這兩種等級機構下,還有一道橋梁InputStreamReader、OutputStreamWriter負責進行InputStream到Reader的適配和由OutputStream到Writer的適配。
java.io.Reader 和 java.io.InputStream 組成了 Java輸入類。Reader 用於讀入16位字元,也就是 Unicode編碼的字元;而 InputStream 用於讀入 ASCII字元和二進制數據。
在 Java中,有不同類型的 Reader 輸入流對應於不同的數據源:
FileReader 用於從文件輸入;
CharArrayReader 用於從程序中的字元數組輸入;
StringReader 用於從程序中的字元串輸入;
PipedReader 用於讀取從另一個線程中的 PipedWriter 寫入管道的數據。
相應的也有不同類型的 InputStream 輸入流對應於不同的數據源:FileInputStream,ByteArrayInputStream,StringBufferInputStream,PipedInputStream。另外,還有兩種沒有對應 Reader 類型的 InputStream 輸入流:
Socket 用於套接字;
URLConnection 用於 URL 連接。
這兩個類使用 getInputStream() 來讀取數據。
相應的,java.io.Writer 和 java.io.OutputStream 也有類似的區別。
㈤ java讀取配置文件的方法(xml)
用的是jdom包
URL url = RederXml.class.getClassLoader().getResource("");
String path = url.toString() + "/config.xml";\\工程種xml的路徑
HashMap<String, String> map = new HashMap<String, String>();
SAXBuilder sax = new SAXBuilder();
Document doc = null;
try {
doc = sax.build(path);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element root = doc.getRootElement();
㈥ JAVA中值的讀寫,在變數、常量、配置文件和資料庫表的取捨
程序中的不可變數,非集合形態的可以寫成靜態常量 集合形態的可以寫成enum類
程序初始化參數,本地可更改變數,數據較少可以存放於properties文件,大量數據可以選擇XML,便於解析與更改
大型的數據集合應存放與資料庫中,資料庫可以良好的管理大型數據集合,並且加快索引速度
單機程序一般都選擇XML為存儲源,或者自定義存儲格式
網路程序才用到資料庫
㈦ java怎樣從配置文件里讀取文本文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Properties;
public class Test
public static void main(String[] args) {
Properties pro = new Properties();
try {
pro.load(new FileInputStream((c:/jdbc.properties")));
} catch (FileNotFoundException e) {
System.out.println("1-FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("2-IOException");
e.printStackTrace();
}
db_driver = pro.getProperty("db_driver");
db_url = pro.getProperty("db_url");
db_userName = pro.getProperty("db_userName");
db_passWord = pro.getProperty("db_passWord");
}
}
-------------------------------------------------------
c:/jdbc.properties 內容
db_driver=aaaa
db_url=bbbb
db_userName=cccc
db_passWord=dddd
----------------------------------------------------------
希望可以幫到你
㈧ JAVA零配置讀取配置文件
packageresources;
importjava.io.IOException;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.Driver;
importjava.sql.SQLException;
importjava.util.Properties;
publicclassDbUtil{
(){
StringdriverClass=null;
StringjdbcUrl=null;
Stringuser=null;
Stringpassword=null;
//讀取類路徑下的jdbc.properties文件,我的配置文件放在src包下
InputStreamin=DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
Propertiesproperties=newProperties();
try{
properties.load(in);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("username");
password=properties.getProperty("password");
Driverdriver=null;
try{
driver=(Driver)Class.forName(driverClass).newInstance();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
Propertiesinfo=newProperties();
info.put("user",user);
info.put("password",password);
//通過Driver的connect方法獲取資料庫連接.
Connectionconnection=null;
try{
connection=driver.connect(jdbcUrl,info);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println("----Thedatabaseisconnected----");
returnconnection;
}
}
jdbc.properties內容如下:
driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@10.91.4.102:1521:orcljdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=cp2
password=cp2test
//調用方法
publicstaticvoidmain(String[]args){
Stringid="111111";
Stringgread="3";
List<Student>sList=newArrayList<Student>();
Connectioncon=DbUtilCR.getConnection();
PreparedStatementpre=null;
ResultSetresult=null;
Stringsql="selects.name,s.agefromstudentswheres.id=?ands.gread=?";
try{
pre=con.prepareStatement(sql);
pre.setString(1,id);//傳參數學號
pre.setString(2,gread);//傳參數年級
result=pre.executeQuery();
System.out.println("執行SQL為:["+sql+"]");
System.out.println("參數為:["+id+","+gread+"]");
while(result.next()){
Studentst=newStudent();
st.setName(result.getString("name"));//與查詢出的欄位或者別名保持一致
st.setAge(result.getString("age"));
sList.add(st);
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
for(inti=0;i<sList.size();i++){
System.out.println("姓名:"+sList.get(i).getName()+" 年齡:"+sList.get(i).getAge());
}
}
㈨ 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中如何設置讀取ini配置文件
//讀取一般的屬性文件
FileInputStreamfin=newFileInputStream("my.ini");//打開文件
Propertiesprops=newProperties();//建立屬性類
props.load(fin);//讀入文件
fin.close();//關閉文件