很多人想知道如何用java读取和修改项目外的properties文件,通过这个程序告诉大家方法,就算把jar文件作出exe 也可以读取和修改properties文件的参数的键和值。
此程序可直接执行,只需将用到的几个property文件放到project的跟目录下即可
package com.start;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;
/**
* 读取properties配置文件信息,修改配置文件内容,并启动使用该配置文件的应用程序
*
* @author www.jianfei5u.com
*/
public class J2MEServerStart extends JFrame {// 配置启动窗口类
private static final long serialVersionUID = -56637913339605867L;
JPanel contentPane;
private String stateServerIP;
private String stateServerPort;
// ***********//程序界面
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField loginName = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField password = new JTextField();
JPanel jPanel2 = new JPanel();
JButton start = new JButton();
JButton quit = new JButton();
public static void main(String[] args) {// 主程序
J2MEServerStart f = new J2MEServerStart();
f.setVisible(true);
}
public J2MEServerStart() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
loadStateLoginInfo();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setResizable(false);
// this.setSize(new Dimension(344, 245));
// 设置窗体位置
this.setLocation(400, 100);
// 设置窗体大小
this.setSize(new Dimension(280, 220));
// 设置窗体标题
this.setTitle("启动服务器并登录集群服务器");
jPanel1.setBounds(new Rectangle(2, 3, 260, 100));
jPanel1.setLayout(null);
jLabel1.setText("请输入登录信息");
jLabel1.setBounds(new Rectangle(5, 7, 190, 18));
jLabel2.setText("用户名");
jLabel2.setBounds(new Rectangle(40, 35, 80, 18));
loginName.setBounds(new Rectangle(100, 35, 90, 22));
jLabel3.setText("密 码");
jLabel3.setBounds(new Rectangle(40, 75, 80, 18));
password.setBounds(new Rectangle(100, 75, 90, 22));
// 确认按钮
jPanel2.setBounds(new Rectangle(2, 100, 260, 80));
jPanel2.setLayout(null);
start.setText("启动");
start.setBounds(new Rectangle(40, 27, 99, 29));
start.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
start_mouseClicked(e);
}
});
// 详细配置按钮
quit.setText("详细配置");
quit.setBounds(new Rectangle(150, 27, 99, 29));
quit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
J2MEServerSet f = new J2MEServerSet();
f.setVisible(true);
}
});
contentPane.add(jPanel1, null);
jPanel1.add(jLabel1, null);
jPanel1.add(jLabel2, null);
jPanel1.add(loginName, null);
jPanel1.add(jLabel3,null);
jPanel1.add(password,null);
contentPane.add(jPanel2, null);
jPanel2.add(start, null);
jPanel2.add(quit, null);
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void start_mouseClicked(MouseEvent e) {// 点击启动按扭
Properties ppt = System.getProperties();
String path = ppt.getProperty("user.dir");
String user = loginName.getText();
String pwd = password.getText();
try {
this.writeProp(path, "StateConfiguration.properties", "ServerLoginName", user);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
this.writeProp(path, "StateConfiguration.properties", "ServerPassword", pwd);
} catch (IOException e1) {
e1.printStackTrace();
}
//severCore.exe是在本项目根目录下的一个可执行文件
String pathAddFile = path + File.separator + "severCore.exe";
Runtime rn = Runtime.getRuntime();
// 启动
try {
rn.exec("cmd /c start " + pathAddFile);
this.dispose();
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
void newuser_mouseClicked(MouseEvent e) {// 清除按纽
}
void quit_mouseClicked(MouseEvent e) {// 关闭按扭
this.dispose();
System.exit(0);
}
/**
* 加载状态服务器配置文件,获得状态服务登录账号和密码
*
* @return
* @throws IOException
*/
public void loadStateLoginInfo() throws IOException {
Properties psys = System.getProperties();
String path = psys.getProperty("user.dir");// 程序所在的路径;
InputStream inputStream = null;
if (new File(path + "\\StateConfiguration.properties").exists()) {
File file = new File(path + "\\StateConfiguration.properties");
inputStream = new FileInputStream(file);
} else {
inputStream = this.getClass().getClassLoader().getResourceAsStream(
"StateConfiguration.properties");
}
Properties p = new Properties();
p.load(inputStream);
loginName.setText(p.getProperty("ServerLoginName"));
password.setText(p.getProperty("ServerPassword"));;
}
// public void loadDBSet() throws IOException{
// if(dbType.get)
// }
/**
* 设置property文件.
*
* @param key
* @param value
* @return String
* @throws IOException
*/
String writeProp(String filePath, String fileName, String key, String value)
throws IOException {
String strResult = "";
String pathAddFile = "";
if (filePath.equals("")) {
pathAddFile = fileName;
} else {
pathAddFile = filePath + File.separator + fileName;
}
File aFile = new File(pathAddFile);
if (!aFile.exists()) {
strResult = "error";
return strResult;
}
InputStream inputStream = null;
try {
inputStream = new FileInputStream(aFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Properties p = new Properties();
p.load(inputStream);
Object s = p.setProperty(key, value);
FileOutputStream output = new FileOutputStream(aFile);
p.store(output, "");
return strResult;
}
}
马上为大家推出 多用户的P2P在线聊天程序 对开发过程进行详细的讲解,该程序时经过本人调试,完全可以运行 ,需要的情参看:http://java161.iteye.com/blog/616113