導航:首頁 > 編程語言 > javapropertiesini

javapropertiesini

發布時間:2024-04-30 21:44:00

java技術:XML配置文件的讀取處理


Java和XML是黃金組合,網上已經有很多文章介紹,XML作為電子商務中數據交換,已經有其不可替代的作用,但是在平時系統開發中,我們不一定都用到數據交換,是不是無法使用XML了?
當然不是,現在已經有一個新趨勢,java程序的配置文件都開始使用XML格式,以前是使用類似windows的INI格式。(Java中也有Propertiesy這樣的類專門處理這樣的屬性配置文件)。使用XML作為Java的配置文件有很多好處,從Tomcat的安裝配置文件和J2ee的配置文件中,我們已經看到XML的普遍應用,讓我們也跟隨流行趨旦飢勢用XML武裝起來。
現在關鍵是如何讀取XML配置模余返文件?有好幾種XML解析器:主要有DOM和SAX ,這些區別網上文章介紹很多。
在apache的XML項目組中,目前有Xerces Xalan Cocoon幾個開毀讓發XML相關技術的project.Tomcat本身使用的是 Sun 的 JAXP,而其XSL Taglib project中使用Xerces解析器。
好了,上面都是比較煩人的理論問題,還是趕快切入XML的配置文件的讀取吧。
在我們的程序中,通常要有一些根據主機環境確定的變數。比如資料庫訪問用戶名和密碼,不同的主機可能設置不一樣。只要更改XML配置文件,就可以正常運行。
localhost
sqlname
username
password
上面這個myenv.xml配置文件一般是放在tomcat的WEB-INF/classes目錄下。
我們編制一個Java程序直接讀取,將dbhost dbuser dbpassword提取出來供其他程序訪問資料庫用。
目前使用SAX比較的多,與DOM主要區別是 SAX是一行一行讀取XML文件進行分析,適合比較大文件,DOM是一次性讀入內存,顯然不能對付大文件。這里我們使用SAX解析,由於SAX解析器不斷在發展,網上有不少文章是針對老版本的。如果你使用JDK1.4 ,可以參考 使用SAX處理XML文檔 一文。這里的程序是根據其改進並且經過實踐調試得來的。
對上面myenv.xml讀取的Java程序:
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;
//使用DefaultHandler的好處 是 不必陳列出所有方法,
public class ConfigParser extends DefaultHandler {
////定義一個Properties 用來存放 dbhost dbuser dbpassword的值
private Properties props;
private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
//構建器初始化props
public ConfigParser() {
this.props = new Properties();
}
public Properties getProps() {
return this.props;
}
//定義開始解析元素的方法. 這里是將中的名稱xxx提取出來.
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
currentValue.delete(0, currentValue.length());
this.currentName =qName;
}
//這里是將之間的值加入到currentValue
public void characters(char[] ch, int start, int length) throws SAXException {
currentValue.append(ch, start, length);
}
//在遇到結束後,將之前的名稱和值一一對應保存在props中
public void endElement(String uri, String localName, String qName) throws SAXException {
props.put(qName.toLowerCase(), currentValue.toString().trim());
}
}
上面的這個解析程序比較簡單吧? 其實解析XML就是這么簡單。
現在我們已經將dbhost dbuser dbpassword的值localhost sqlname username password提取了出來。但是這只是在在解析器內部,我們的程序還不能訪問。需要再編制一個程序。
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import .URL;
public class ParseXML{
//定義一個Properties 用來存放 dbhost dbuser dbpassword的值
private Properties props;
//這里的props
public Properties getProps() {
return this.props;
}
public void parse(String filename) throws Exception {
//將我們的解析器對象化
ConfigParser handler = new ConfigParser();
//獲取SAX工廠對象
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
//獲取SAX解析
SAXParser parser = factory.newSAXParser();
//得到配置文件myenv.xml所在目錄. tomcat中是在WEB-INF/classes
//下例中BeansConstants是用來存放xml文件中配置信息的類,可以自己代替或定義
URL confURL = BeansConstants.class.getClassLoader().getResource(filename);
try
{
//將解析器和解析對象myenv.xml聯系起來,開始解析
parser.parse(confURL.toString(), handler);
//獲取解析成功後的屬性 以後 我們其他應用程序只要調用本程序的props就可以提取出屬性名稱和值了
props = handler.getProps();
}finally{
factory=null;
parser=null;
handler=null;
}
}
}
由於我們的XML文件是使用最簡單的形式 ,因此解析器相對簡單,但是這已經足夠對付我們的配置文件了。

Ⅱ java 中讀取一個ini文件,報錯,找不到文件,文件在src下

Properties p = new Properties();
p.load(ClassLoader.getSystemClassLoader().getResource("inaaa.ini").openStream());

Ⅲ 幫忙寫個JAVA 讀寫ini配置文件小程序!!!!!

其實使用 JDK 裡面提供的 Properties 最方便。 相關使用方法可以自己去查看 JDK 的API文檔。 package proct;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;public class IniReader {

private Properties properties = new Properties();

private String iniPath = "test/proct/pro.ini"; //ini 文件的路徑

private JFrame jFrame = new JFrame("讀取配置示例");

private JLabel jLabel1 = new JLabel("用戶登錄IP");
private JTextField jTextField1 = new JTextField(30);

private JLabel jLabel2 = new JLabel("埠號");
private JTextField jTextField2 = new JTextField(30);

private JLabel jLabel3 = new JLabel("TQ終端IP");
private JTextField jTextField3 = new JTextField(30);

private JLabel jLabel4 = new JLabel("埠號");
private JTextField jTextField4 = new JTextField(30);

private JLabel jLabel5 = new JLabel("WM終端IP");
private JTextField jTextField5 = new JTextField(30);

private JLabel jLabel6 = new JLabel("埠號");
private JTextField jTextField6 = new JTextField(30);

private JButton jButton1 = new JButton("取消");

private JButton jButton2 = new JButton("確定");

private void showFrame(){
try {
File file = new File(iniPath);
System.out.println(file.getAbsolutePath());
properties.load(new FileInputStream(iniPath));
} catch (FileNotFoundException e) {
System.out.println("找不到該文件");
JOptionPane.showMessageDialog(null, "保存信息出錯!");
return;
} catch (IOException e) {
System.out.println("文件讀取錯誤");
JOptionPane.showMessageDialog(null, "保存信息出錯!");
return;
}
jTextField1.setText(properties.getProperty("UserLogin"));
jTextField2.setText(properties.getProperty("Userport"));
jTextField3.setText(properties.getProperty("TQterminal"));
jTextField4.setText(properties.getProperty("TQport"));
jTextField5.setText(properties.getProperty("VMterminal"));
jTextField6.setText(properties.getProperty("VMport"));

jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextField1.setText(properties.getProperty("UserLogin"));
jTextField2.setText(properties.getProperty("Userport"));
jTextField3.setText(properties.getProperty("TQterminal"));
jTextField4.setText(properties.getProperty("TQport"));
jTextField5.setText(properties.getProperty("VMterminal"));
jTextField6.setText(properties.getProperty("VMport"));
}
});

jButton2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
properties.setProperty("UserLogin", jTextField1.getText());
properties.setProperty("Userport", jTextField2.getText());
properties.setProperty("TQterminal", jTextField3.getText());
properties.setProperty("TQport", jTextField4.getText());
properties.setProperty("VMterminal", jTextField5.getText());
properties.setProperty("VMport", jTextField6.getText());
try {
properties.store(new FileOutputStream(iniPath),"");
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("保存信息出錯");
JOptionPane.showMessageDialog(jFrame, "保存信息出錯!");
}
JOptionPane.showMessageDialog(jFrame, "保存成功!");
}

});
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jLabel1.setBounds(10, 40, 80, 30);
jTextField1.setBounds(100, 40, 80, 30);
jLabel2.setBounds(210, 40, 80, 30);
jTextField2.setBounds(300, 40, 80, 30);

jLabel3.setBounds(10, 80, 80, 30);
jTextField3.setBounds(100, 80, 80, 30);
jLabel4.setBounds(210, 80, 80, 30);
jTextField4.setBounds(300, 80, 80, 30);

jLabel5.setBounds(10, 120, 80, 30);
jTextField5.setBounds(100, 120, 80, 30);
jLabel6.setBounds(210, 120, 80, 30);
jTextField6.setBounds(300, 120, 80, 30);

jFrame.getContentPane().setLayout(null);

jFrame.getContentPane().add(jLabel1);
jFrame.getContentPane().add(jLabel2);
jFrame.getContentPane().add(jLabel3);
jFrame.getContentPane().add(jLabel4);
jFrame.getContentPane().add(jLabel5);
jFrame.getContentPane().add(jLabel6);

jFrame.getContentPane().add(jTextField1);
jFrame.getContentPane().add(jTextField2);
jFrame.getContentPane().add(jTextField3);
jFrame.getContentPane().add(jTextField4);
jFrame.getContentPane().add(jTextField5);
jFrame.getContentPane().add(jTextField6);

jButton1.setBounds(100,160,60,30);
jButton2.setBounds(230,160,60,30);

jFrame.getContentPane().add(jButton1);
jFrame.getContentPane().add(jButton2);

jFrame.setBounds(200, 200, 400, 300);
jFrame.setVisible(true);

}

public static void main(String[] args) {
new IniReader().showFrame();
}}
經測試,可用,正常。就是文件路徑你自己配好。

閱讀全文

與javapropertiesini相關的資料

熱點內容
重慶會展收入數據在哪裡找得 瀏覽:364
萬能鑰匙掃一掃的版本 瀏覽:888
移動叔叔一鍵root工具電腦版 瀏覽:493
如何設計求和編程 瀏覽:762
mac雙系統win可以自己升級嗎 瀏覽:590
學籍管理系統程序 瀏覽:527
怎樣升級1032beta5 瀏覽:484
如何重啟網路適配器 瀏覽:602
gentoolinux下載 瀏覽:327
系統數據里沒有bilibili的文件夾 瀏覽:809
大數據在醫院的應用 瀏覽:568
大數據鐵路安全問題 瀏覽:676
帶圓弧的螺紋怎麼編程 瀏覽:378
mw文件如何轉換為pdf格式 瀏覽:556
新片場app怎麼申請加入創作人 瀏覽:729
手冊編程軟體哪裡下載 瀏覽:191
中興運城大數據產業園 瀏覽:792
數控編程怎麼開直角三角形 瀏覽:100
文件全名是什麼 瀏覽:788
種子文件都是什麼格式 瀏覽:20

友情鏈接