Java自带了处理配置文件的类:
import java.util.Properties;
配置文件li.deyong的内容如下:
ConnectionString=jdbc:microsoft:sqlserver://ldy:1433;DatabaseName=dg
UserName=sa
UserPass=abc
IP=192.168.1.27
Port=13000
使用到的对象:
public static Hashtable htConfig=new Hashtable();
/**
* 配置文件设置
*/
static Properties property=new Properties();
读取配置文件并把相关信息存入哈希表:
htConfig.put("ConnectionString",property.getProperty("ConnectionString"));
htConfig.put("UserName",property.getProperty("UserName"));
htConfig.put("UserPass",property.getProperty("UserPass"));
htConfig.put("IP",property.getProperty("IP"));
htConfig.put("Port",property.getProperty("Port"));
使用配置参数时从哈希表时读取:
/**
* 读取配置的IP和端口号
*/
int port=Integer.parseInt(htConfig.get("Port").toString());
InetAddress ip=InetAddress.getByName(htConfig.get("IP").toString());
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ServerSocket ss = ssc.socket();
InetSocketAddress isa = new InetSocketAddress(ip,port);
ss.bind(isa);