读取和修改properties文件并启动exe程序

很多人想知道如何用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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值