练习题(二)
4. 字符串練習題(6)中的數據存在文件data.txt中,模板存在文件template.tmpl中,編寫函數:
public static String composeMessage(String dataFileName, String templateFileName)
實現將數據文件和模板文件的內容組織成完整消息。
在此基礎上實現一個命令行工具,可將數據和模板文件組成的消息存到指定文件中。要求能準確報告出錯誤原因。命令語法:
java cm -m dataFileName -v templateFileName -o outputFileName
限時:學習20分,編碼30分,測試30
---------------------------------------------------------------------------
#客戶號 姓名 所述機構號 性別 帳號 發生時間 發生額
000001|劉德華|0000|1|4155990188888888|20060720200005|300.00
000201|曉龍|0002|1|4155990199999999|20060720200005|500.00
000101|黃曉明|0012|1|4155990100000000|20060720200005|1000.50
000101|張東健|0012|1|4155990155555555|20060720200005|600.99
000301|梁朝偉|0013|0|4155990111111111|20060722201005|5000.00
000001|劉德華|0000|1|4155990188888888|20060725200005|200.00
---------------------------------------------------------------------------
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.util.ArrayList;
9 import java.util.List;
10
11 class Test {
12 private String customerInfo;
13
14 public Test(String customerInfo) {
15 super();
16 this.customerInfo = customerInfo;
17 }
18
19 public String getCustomerInfo() {
20 return customerInfo;
21 }
22
23 public void setCustomerInfo(String customerInfo) {
24 this.customerInfo = customerInfo;
25 }
26
27 @Override
28 public String toString() {
29 return this.getCustomerInfo() + "\n";
30 }
31
32 }
33
34 public class TestMessage {
35
36 public static void main(String[] args) {
37 // TODO Auto-generated method stub
38 System.out.println(composeMessage("c:" + File.separator + "data.txt",
39 "c:" + File.separator + "template.tmpl"));
40 }
41
42 public static String composeMessage(String dataFileName,
43 String templateFileName) {
44 File file1 = new File(templateFileName);
45 File file2 = new File(dataFileName);
46 List<Test> list = new ArrayList<Test>();
47 StringBuffer bf = new StringBuffer();
48 BufferedReader br = null;
49 String tempStr = "";
50 try {
51 br = new BufferedReader(new FileReader(file1));
52 } catch (FileNotFoundException e) {
53 e.printStackTrace();
54 }
55 try {
56 while ((tempStr = br.readLine()) != null) {
57 list.add(new Test(tempStr));
58 }
59 } catch (IOException e) {
60 e.printStackTrace();
61 }
62 try {
63 br = new BufferedReader(new FileReader(file2));
64 } catch (FileNotFoundException e) {
65 e.printStackTrace();
66 }
67 try {
68 while ((tempStr = br.readLine()) != null) {
69 list.add(new Test(tempStr));
70 }
71 } catch (IOException e) {
72 e.printStackTrace();
73 }
74 try {
75 br.close();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 Object[] o = list.toArray();
80 for (Object ot : o) {
81 bf.append(ot.toString());
82 }
83 return bf.toString();
84 }
85
86 }
?
View Code 1 package com.study.pratice;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.util.ArrayList;
9 import java.util.List;
10
11 class Message {
12 private String cuId;
13 private String cuName;
14 private String insNum;
15 private String cusSex;
16 private String account;
17 private String occTime;
18 private String amount;
19
20 public Message(String[] str, int n) {
21 if (n == 7) {
22 this.cuId = str[0];
23 this.cuName = str[1];
24 this.insNum = str[2];
25 this.cusSex = str[3];
26 this.account = str[4];
27 this.occTime = str[5];
28 this.amount = str[6];
29 }
30
31 }
32
33 public String getCuId() {
34 return cuId;
35 }
36
37 public void setCuId(String cuId) {
38 this.cuId = cuId;
39 }
40
41 public String getCuName() {
42 return cuName;
43 }
44
45 public void setCuName(String cuName) {
46 this.cuName = cuName;
47 }
48
49 public String getInsNum() {
50 return insNum;
51 }
52
53 public void setInsNum(String insNum) {
54 this.insNum = insNum;
55 }
56
57 public String getCusSex() {
58 return cusSex;
59 }
60
61 public void setCusSex(String cusSex) {
62 this.cusSex = cusSex;
63 }
64
65 public String getAccount() {
66 return account;
67 }
68
69 public void setAccount(String account) {
70 this.account = account;
71 }
72
73 public String getOccTime() {
74 return occTime;
75 }
76
77 public void setOccTime(String occTime) {
78 this.occTime = occTime;
79 }
80
81 public String getAmount() {
82 return amount;
83 }
84
85 public void setAmount(String amount) {
86 this.amount = amount;
87 }
88
89 @Override
90 public String toString() {
91 return getCuId() + " " + getCuName() + " " + getInsNum() + " "
92 + getCusSex() + " " + getAccount() + " " + getOccTime() + " "
93 + getAmount();
94 }
95
96 }
97
98 public class ComposeFile {
99
100 public static String composeMessage(String dataFileName,
101 String templateFileName) {
102 File file1 = new File(dataFileName);
103 File file2 = new File(templateFileName);
104 List<Message> list = new ArrayList<Message>();
105 StringBuffer bf = new StringBuffer();
106 BufferedReader br = null;
107 String s = "";
108 if (file1.exists() && file2.exists()) {
109 try {
110 br = new BufferedReader(new FileReader(file2));
111 } catch (FileNotFoundException e) {
112 e.printStackTrace();
113 }
114 try {
115 while ((s = br.readLine()) != null) {
116 String[] tempStr = s.split(" ");
117 list.add(new Message(tempStr, tempStr.length));
118 }
119 } catch (IOException e) {
120 e.printStackTrace();
121 }
122 try {
123 br = new BufferedReader(new FileReader(file1));
124 } catch (FileNotFoundException e) {
125 e.printStackTrace();
126 }
127 try {
128 while ((s = br.readLine()) != null) {
129 String[] tempStr = s.split("\\|");
130 list.add(new Message(tempStr, tempStr.length));
131
132 }
133 } catch (IOException e) {
134 e.printStackTrace();
135 }
136 try {
137 br.close();
138 } catch (IOException e) {
139 e.printStackTrace();
140 }
141 for (Message me : list) {
142 bf.append(me.toString()).append("\n");
143 }
144 }
145 return bf.toString();
146 }
147
148 public static void main(String[] args) {
149
150 System.out.println(composeMessage("c:\\data.txt", "c:\\template.tmpl"));
151
152 }
153
154 }
5. 配置文件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的功能。
限時:學習40分,編碼60分,測試30
2 import java.io.File;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8 class ServiceInfo {
9 private String serviceName;
10 private String address;
11 private int port;
12 private String serviceClassName;
13 private String description;
14
15 public ServiceInfo(String[] str, int n) {
16 if (n == 5) {
17 this.serviceName = str[0];
18 this.address = str[1];
19 this.port = Integer.parseInt(str[2]);
20 this.serviceClassName = str[3];
21 this.description = str[4];
22 } else {
23 System.out.println("對象創建失敗!");
24 }
25 }
26 public String getServiceName() {
27 return serviceName;
28 }
29 public void setServiceName(String serviceName) {
30 this.serviceName = serviceName;
31 }
32 public String getAddress() {
33 return address;
34 }
35 public void setAddress(String address) {
36 this.address = address;
37 }
38 public int getPort() {
39 return port;
40 }
41 public void setPort(int port) {
42 this.port = port;
43 }
44 public String getServiceClassName() {
45 return serviceClassName;
46 }
47 public void setServiceClassName(String serviceClassName) {
48 this.serviceClassName = serviceClassName;
49 }
50 public String getDescription() {
51 return description;
52 }
53 public void setDescription(String description) {
54 this.description = description;
55 }
56 @Override
57 public String toString() {
58 return "服務名稱:" + getServiceName() + " 目的地址:" + getAddress() + " 端口號:"
59 + getPort() + " 服務類名:" + getServiceClassName() + " 系統名稱:"
60 + getDescription();
61 }
62
63 }
64 interface ServiceInfoManager {
65 public ServiceInfo getServiceInfo(String serviceName);
66
67 }
68 public class Practice05 implements ServiceInfoManager
69 {
70 public ServiceInfo getServiceInfo(String serviceName) {
71 List<ServiceInfo> reList=this.readFile("c:"+File.separator+"service.txt");
72 for(ServiceInfo si:reList)
73 {
74 if(si.getServiceName().equals(serviceName.toUpperCase()))
75 {
76 return si;
77 }
78 }
79 return null;
80 }
81 public List<ServiceInfo> readFile(String fileName)
82 {
83 BufferedReader br=null;
84 int n=0;
85 String str="";
86 List<ServiceInfo> list=new ArrayList<ServiceInfo>();
87 try {
88 br=new BufferedReader(new FileReader(new File(fileName)));
89 } catch (FileNotFoundException e) {
90 e.printStackTrace();
91 }
92 try {
93 while((str=br.readLine())!=null)
94 {
95 n++;
96 if(n==1) continue;
97 String[] tempStr=str.split("\\s{1,}");
98 for(int i=0;i<tempStr.length;i++)
99 {
100 list.add(new ServiceInfo(tempStr,tempStr.length));
101 }
102 }
103 } catch (IOException e) {
104 e.printStackTrace();
105 }
106 return list;
107 }
108 public static void main(String[] args)
109 {
110 Practice05 sim=new Practice05();
111 ServiceInfo b=null;
112 if((b=sim.getServiceInfo("host"))!=null)
113 {
114 System.out.println(b);
115 }
116 else
117 {
118 System.out.println("沒有找到!");
119 }
120 }
121 }
轉載于:https://www.cnblogs.com/xiongyu/archive/2011/11/14/2248115.html
總結
- 上一篇: 关于利用VS2008创建项目遇到的小困惑
- 下一篇: 用filter实现web程序的统一认证