使用mina框架访问服务器需要一些配置文件供mina包读取。一般的mina包都要经过修改才可以被使用,比如服务器ip与端口号等,在实际开发中都会通过config.properties保存,而很少直接new Socket("127.0.01");
config.properties文件读取:
配置文件:aaa.joe
- host=127.0.0.1
- port=8080
- taskqueuesize=15
Main类:Main.class
- package com.example.readproperties;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
-
- public class Main {
-
- private static Properties properties;
-
-
-
-
- public static void main(String[] args) {
- InputStream in = Main.class.getClassLoader().getResourceAsStream(
- "aaa.joe");
- properties = new Properties();
- try {
- properties.load(in);
-
-
-
-
-
-
- System.out.println("主机:" + getHost() + "\n" + "端口号:" + getPort()
- + "\n" + "最大线程数:" + getTaskqueuesize());
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static String getHost() {
- return "" + properties.get("host");
- }
-
- public static String getPort() {
- return "" + properties.get("port");
- }
-
- public static String getTaskqueuesize() {
- return "" + properties.get("taskqueuesize");
- }
- }
备注1:从项目src根目录下读取配置文件,只要文件名正确,后缀并不影响读取到的数据。不过一般使用.properties做后缀。
免积分Demo源码