配置文件service.txt的内容如下:
------------------------------------------------------------
#服务名 目的地址 端口号 服务类名 系统名称
HOST localhost 9870 TransferService 后台主机
HIPAY localhost 9870 BJElecToHipayService 应用服务器
------------------------------------------------------------
一行是一个service的配置,每行5列,列间以空格或tab字符分隔。
#为注释符号,即首字符是#的行为注释
类ServiceInfo如下:
public class ServiceInfo{
private String serviceName;
private String address;
private int port;
private String serviceClassName;
private String description;
//get/set method
}
一个ServiceInfo对象存储一行服务配置信息。
类ServiceInfoManager的接口如下:
public Interface ServiceInfoManager{
public ServiceInfo getServiceInfo(String serviceName);
}
请编写类实现ServiceInfoManager接口,该类能自动读取service.txt配置文件生成ServiceInfo对象,并对外提供最快的按serviceName检索ServiceInfo的功能。
1 package test.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 11 class ServiceInfo{ 12 private String serviceName; 13 private String address; 14 private int port; 15 private String serviceClassName; 16 private String description; 17 18 public ServiceInfo(String[] str,int n){ 19 if (5 == n){ 20 this.serviceName = str[0]; 21 this.address = str[1]; 22 this.port = Integer.parseInt(str[2]); 23 this.serviceClassName = str[3]; 24 this.description = str[4]; 25 } 26 else { 27 System.out.println("对象创建失败!"); 28 System.exit(1); //发生异常,终止程序 29 } 30 } 31 32 public void setServiceName(String serviceName){ 33 this.serviceName = serviceName; 34 } 35 36 public String getServiceName(){ 37 return serviceName; 38 } 39 40 public void setAddress(String address){ 41 this.address = address; 42 } 43 44 public String getAddress(){ 45 return address; 46 } 47 48 public void setPort(int port){ 49 this.port = port; 50 } 51 52 public int getPort(){ 53 return port; 54 } 55 56 public void setServiceClassName(String serviceClassName){ 57 this.serviceClassName = serviceClassName; 58 } 59 60 public String getServiceClassName(){ 61 return serviceClassName; 62 } 63 64 public void setDescription(String description){ 65 this.description = description; 66 } 67 68 public String getDescription(){ 69 return description; 70 } 71 72 public String toString(){ 73 return ("服务名称:"+getServiceName()+" 目的地址:"+getAddress()+" 端口号:"+getPort()+ 74 " 服务类型名称:"+getServiceClassName()+" 系统名称:"+getDescription()); 75 } 76 } 77 78 interface ServiceInfoManager{ 79 public ServiceInfo getServiceInfo(String serviceName); 80 } 81 82 public class Service implements ServiceInfoManager { 83 public ServiceInfo getServiceInfo(String serviceName){ 84 String filePath = System.getProperty("user.dir") 85 + "\\src\\test\\util\\service.txt"; 86 ArrayList<ServiceInfo> relist = this.readFile(filePath);//生成ServiceInfo对象队列 87 for(ServiceInfo si:relist){ 88 if(si.getServiceName().equals(serviceName.toUpperCase())){ 89 return si; 90 } 91 } 92 return null; 93 } 94 95 public ArrayList<ServiceInfo> readFile(String fileName){// 读取文本内容,并生成对象存放在List队列中 96 BufferedReader br = null; 97 int n = 0; 98 String str = new String(); 99 ArrayList<ServiceInfo> list = new ArrayList<ServiceInfo>(); 100 try{ 101 br = new BufferedReader(new FileReader(new File(fileName))); 102 } 103 catch(FileNotFoundException e){ 104 e.printStackTrace(); 105 return null; 106 } 107 try{ 108 while((str=br.readLine())!= null){ 109 n++; 110 if(1 == n) 111 continue; 112 String[] tempStr = str.split("\\s{1,}"); 113 for(int i =0;i<tempStr.length;i++){ 114 list.add(new ServiceInfo(tempStr,tempStr.length)); 115 } 116 } 117 return list; 118 } 119 catch (IOException ee){ 120 ee.printStackTrace(); 121 return null; 122 } 123 finally{ 124 if (null != br){ 125 try{ 126 br.close(); 127 } 128 catch (IOException eee){ 129 eee.printStackTrace(); 130 } 131 } 132 } 133 } 134 135 public static void main(String[] args){ 136 Service sim = new Service(); 137 ServiceInfo b = null; 138 System.out.print("请输入要查找的协议名称:"); 139 BufferedReader temp = new BufferedReader(new InputStreamReader(System.in)); 140 String str; 141 try { 142 str = temp.readLine();//处理readFile异常 143 if((b=sim.getServiceInfo(str))!=null){ 144 System.out.println(b); 145 } 146 else{ 147 System.out.println("没有找到"+str+"!"); 148 } 149 } 150 catch (IOException e) { 151 e.printStackTrace(); 152 } 153 finally{ 154 if( null != temp){ 155 try { 156 temp.close(); 157 } 158 catch (IOException e) { 159 e.printStackTrace(); 160 } 161 } 162 } 163 } 164 }