导航:首页 > 版本升级 > java修改配置文件

java修改配置文件

发布时间:2022-12-19 21:56:37

java 读取和修改本地xml配置文件

你看一下这个吧.不需要导入特别的包,直接用API中的DOM方式解析,
并保存的.

-------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class XmlApp extends JFrame implements ActionListener, ItemListener {
private JTextField textField;
private JComboBox comboBox;
private Map<String, String> content = null;
private Document doc;
private String file = "H:\\content.xml";

public XmlApp() {

getContentPane().setLayout(null);

comboBox = new JComboBox();
comboBox.setBounds(110, 10, 112, 21);
comboBox.addItemListener(this);
getContentPane().add(comboBox);

textField = new JTextField();
textField.setBounds(110, 41, 112, 21);
getContentPane().add(textField);
textField.setColumns(10);

JButton btnSave = new JButton("Save");
btnSave.setBounds(252, 40, 93, 23);
btnSave.addActionListener(this);
getContentPane().add(btnSave);

JLabel lblNode = new JLabel("Node");
lblNode.setBounds(10, 13, 90, 15);
getContentPane().add(lblNode);

JLabel lblValue = new JLabel("value");
lblValue.setBounds(10, 44, 90, 15);
getContentPane().add(lblValue);
init();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(400, 120);
setLocationRelativeTo(null);
setVisible(true);
}

public void init() {
content = new HashMap<String, String>();
DocumentBuilderFactory factory;
try {
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(file);
loopNodes(doc.getFirstChild(), "", false);
textField.setText(content.get(comboBox.getSelectedItem()));
} catch (Exception e) {
System.out.println("e = " + e.getMessage());
}
}

public void loopNodes(Node node, String name, boolean isSave) {

NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node tmp = list.item(i);
if (tmp instanceof Element) {
if (tmp.hasChildNodes()) {
loopNodes(tmp, tmp.getNodeName(), isSave);
}
}
if (tmp instanceof Text) {
String value = tmp.getNodeValue();
if (value != null && !"".equals(value.trim())) {
if (isSave) {
tmp.setNodeValue(content.get(name));
} else {
comboBox.addItem(name);
content.put(name, value.trim());
}
}
}
}
}

public static void main(String[] args) {
new XmlApp();
}

public void actionPerformed(ActionEvent e) {
String value = textField.getText();
content.put((String) comboBox.getSelectedItem(), value);
loopNodes(doc, "", true);
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(file));
transformer.transform(source, result);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
textField.setText(content.get(e.getItem()));
}
}
}

② java--swing:实现xml配置文件的修改,没学到swing,所以啥都不会,也不知道如何通过按钮去修改

需要知道你是XML内容是什么
还是说,做一个界面,能读值,能改值 就可以,不用客内容?

③ java 修改系统配置文件内容

不要直接访问配置文件,在启动初始化时,把配置文件etc/sysctl.conf文件读取到一个内存中的HashMap里面去,可以使用单例模式实现,所有的添加、修改、访问全在内存中的HashMap中进行。

启动系统时,加载配置文件到对象,去判断重复等等。
退出系统时,将对象,重新覆盖一下原有的配置文件
运行时所有的修改,全部针对内存中的对象操作

④ java窗体程序 打包jar后 怎么动态修改xml配置信息,请问哪位大神会啊

这种做法很不好呀,应该在窗体程序中增加一个配置文件,原包中的xml文件做为原始配置不动,窗体程序运行时可做一个菜单项“配置”,新做的配置修改保存到本地一个xml文件中,以后启动窗体程序时先检查有没有这个本地xml文件,有则从它里面读入配置,否则从类路径的jar中读取配置

⑤ java热部署:tomcat运行中,动态修改配置文件(java文件)中的static属性并生效

<Context path="/tomcatTest" reloadable="true" docBase="E:\workplace\testProject\WebRoot"/>
第一个是容器里的项目path 要加/
第二个参数是你的workplace的路径,一般是到webroot

写个context.xml文件,放到项目的META-INF里.context.xml头部像上面那样写就可以

⑥ java 向ini配置文件中写入值。

Java读取和修改ini配置文件,参考如下:
/*
* ConfigurationFile.java
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 这是个配置文档操作类,用来读取和配置ini配置文档
* @author 由月
* @version 2004-08-18
* @修改 2008-05-22
*/
public final class ConfigurationFile {
/**
* 从ini配置文档中读取变量的值
* @param file 配置文档的路径
* @param section 要获取的变量所在段名称
* @param variable 要获取的变量名称
* @param defaultValue 变量名称不存在时的默认值
* @return 变量的值
* @throws IOException 抛出文档操作可能出现的io异常
*/
public static String getProfileString(
String file,
String section,
String variable,
String defaultValue)
throws IOException {
String strLine, value = "";
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
boolean isInSection = false;
try {
while ((strLine = bufferedReader.readLine()) != null) {
strLine = strLine.trim();
//strLine = strLine.split("[;]")[0];
Pattern p;
Matcher m;
p = Pattern.compile("\\["+section+"\\]");
m = p.matcher((strLine));
if (m.matches()) {
p = Pattern.compile("\\["+section+"\\]");
m = p.matcher(strLine);
if (m.matches()) {
isInSection = true;
} else {
isInSection = false;
}
}
if (isInSection == true) {
strLine = strLine.trim();
String[] strArray = strLine.split("=");
if (strArray.length == 1) {
value = strArray[0].trim();
if (value.equalsIgnoreCase(variable)) {
value = "";
return value;
}
} else if (strArray.length == 2) {
value = strArray[0].trim();
if (value.equalsIgnoreCase(variable)) {
value = strArray[1].trim();
return value;
}
} else if (strArray.length > 2) {
value = strArray[0].trim();
if (value.equalsIgnoreCase(variable)) {
value = strLine.substring(strLine.indexOf("=") + 1).trim();
return value;
}
}
}
}
} finally {
bufferedReader.close();
}
return defaultValue;
}

阅读全文

与java修改配置文件相关的资料

热点内容
008神器破解版使用教程 浏览:974
word2007密码设置 浏览:593
iPhone5解锁密码格图案 浏览:392
微信文件怎么填 浏览:87
燕十八老师精通mysql视频教程 浏览:255
汽车保养数据怎么清 浏览:629
pdf文件图像打不开 浏览:176
msp430时钟程序 浏览:660
查看sd卡文件系统格式 浏览:696
c盘中显示隐藏文件 浏览:951
苹果升级系统白屏 浏览:136
三菱gxplc编程软件如何使用 浏览:710
海康威视手机app怎么看不了 浏览:482
wordpress下载中心插件 浏览:402
微信限制字数是多少 浏览:20
策划输出主要从哪些文件来 浏览:174
网络营销找什么工作 浏览:372
tcl匹配文件名的正则表达式 浏览:461
音频文件数据量为何8 浏览:534
有哪些分享学习的网站 浏览:174

友情链接