`

读取和修改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

 

2
0
分享到:
评论

相关推荐

    如何编写批处理文件批处理文件批处理文件

    简明批处理教程22009年10月20日 星期二 下午 05:35 最近对于批处理技术的探讨比较热,也有不少好的批处理程序发布,但是如果没有一定的相关知识恐怕不容易看懂和理解这些批处理文件,也就更谈不上自己动手编写了,古...

    nacosdemo:nacos配置演示消费者提供程序

    nacos配置演示消费者提供程序nacos注册中心生产者消费者配置动态修改nacos管理控制台编辑属性文件动态发布给业务系统 注意:nacos本地安装一个 (一)windows开发环境就单机模式启动就行,如下 startup.cmd -m ...

    vss如何使用(图解)

    注:如果用户已经为文件设置了工作文件夹,VSS会将该文件的一个COPY放入你的工作文件夹并打开文件,让用户进行修改和编辑;如果用户还没有为文件设置工作文件夹,VSS系统会提醒用户设置工作文件夹,用户可根据系统...

    visual source safe 教程

    注:如果用户已经为文件设置了工作文件夹,VSS会将该文件的一个COPY放入你的工作文件夹并打开文件,让用户进行修改和编辑;如果用户还没有为文件设置工作文件夹,VSS系统会提醒用户设置工作文件夹,用户可根据系统...

    Visual stdio source safe 教程

    注:如果用户已经为文件设置了工作文件夹,VSS会将该文件的一个COPY放入你的工作文件夹并打开文件,让用户进行修改和编辑;如果用户还没有为文件设置工作文件夹,VSS系统会提醒用户设置工作文件夹,用户可根据系统...

    Java范例开发大全 (源程序)

     实例221 改变Properties文件中的键值 399  第13章 多线程编程(教学视频:121分钟) 405  13.1 多线程的五种基本状态 405  实例222 启动线程 405  实例223 参赛者的比赛生活(线程休眠唤醒) 407  实例...

    Java范例开发大全(全书源程序)

    实例221 改变Properties文件中的键值 399 第13章 多线程编程(教学视频:121分钟) 405 13.1 多线程的五种基本状态 405 实例222 启动线程 405 实例223 参赛者的比赛生活(线程休眠唤醒) 407 实例224 资源搜索...

    超级有影响力霸气的Java面试题大全文档

    并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。 EJB包括Session Bean、Entity Bean、Message Driven Bean,基于JNDI、RMI、JAT等技术实现。 SessionBean在J2EE应用程序中被用来完成...

    java 面试题 总结

    并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。 EJB包括Session Bean、Entity Bean、Message Driven Bean,基于JNDI、RMI、JAT等技术实现。 SessionBean在J2EE应用程序中被用来完成...

    ARCH4系统开发指南

    2.4.1.1 确认mail.properties文件设置正确 12 2.4.1.2 确认applicationContext-mail.xml文件设置正确 12 2.4.1.3 确认applicationContext-service.xml文件设置正确 12 2.4.2 编写使用mailService的代码 12 2.5 如何...

    Activiti6.0教程例子下载

    例如开发一个系统最关键的部分不是系统的界面,也不是和数据库之间的信息交换,而是如何根据业务逻辑开发出符合实际需要的程序逻辑并确保其稳定性、易维护性和弹性。 比如你的系统中有一个任务流程,一般情况下这个...

    java范例开发大全(pdf&源码)

    实例221 改变Properties文件中的键值 399 第13章 多线程编程(教学视频:121分钟) 405 13.1 多线程的五种基本状态 405 实例222 启动线程 405 实例223 参赛者的比赛生活(线程休眠唤醒) 407 实例224 资源搜索并下载...

    java范例开发大全源代码

     实例139 利用POI读取Word文件中的内容 208  7.3 字符流 209  实例140 按顺序创建文件 210  实例141 按顺序读取文件 211  实例142 追加文件内容 211  实例143 只显示文件中指定的字符 214  实例...

    java范例开发大全

    实例221 改变Properties文件中的键值 399 第13章 多线程编程(教学视频:121分钟) 405 13.1 多线程的五种基本状态 405 实例222 启动线程 405 实例223 参赛者的比赛生活(线程休眠唤醒) 407 实例224 资源搜索并下载...

    大数据非关系型数据库课程设计基于Scala的交通拥堵预测源码+项目说明.zip

    **f)** 启动zookeeper,kafka,并创建kafka主题,检查主题存在性 **g)** 将数据发送至kafka并使用kafka console-consumer进行检测 ### **编写消费者** **思路:** **a**)新建子工程:tf_consumer **b)** 配置...

Global site tag (gtag.js) - Google Analytics