导航:首页 > 编程语言 > 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相关的资料

热点内容
学编程考什么证有用 浏览:847
有哪些折扣券的app 浏览:962
mastercamt型螺纹怎么编程 浏览:349
网易云音乐与大数据库 浏览:551
文件夹目录如何生成目录结构图 浏览:620
网络技术毕设 浏览:146
ps文件转曲pdf格式 浏览:990
移动官方网站如何打开 浏览:257
qq农场升级省级土地 浏览:913
tpwr886nv20升级 浏览:912
写文件编码格式 浏览:588
如何在qq浏览器添加常用网站 浏览:861
定量数据是什么资料 浏览:675
查看榆次房屋备案证明在哪个网站 浏览:291
说明书word文件下载 浏览:150
word文字粘贴后有彩色星 浏览:1000
苹果电脑如何只装win7系统文件夹 浏览:703
云建城201557号文件内容 浏览:872
什么是网络营销刺激反应战略 浏览:115
大数据线下测试 浏览:147

友情链接