根据文件data.txt和模板文件template.tmpl,实现将数据文件和模板文件的合并,并保存在输出文件
字符串练习题(6)中的数据存在文件data.txt中,模板存在文件template.tmpl中,编写函数:
public static String composeMessage(String dataFileName, String templateFileName)
实现将数据文件和模板文件的内容组织成完整消息。
在此基础上实现一个命令行工具,可将数据和模板文件组成的消息存到指定文件中。要求能准确报告出错误原因。命令语法:
java cm -m dataFileName -v templateFileName -o outputFileName
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.FileWriter; 8 import java.io.IOException; 9 10 public class ComposeMessage { 11 public static void composeMessage(String dataFileName, String templateFileName, 12 String outputFileName){ 13 StringBuffer tempStr = new StringBuffer(); 14 String data = new String(); 15 String[] tempData = new String[7]; 16 17 TransRecordManager transRecMan = new TransRecordManager(); 18 //获取文件data.txt的数据,返回并保存在数组temp中 19 TransRecord[] tempRec = transRecMan.getAll(); 20 String[] result = new String[tempRec.length]; 21 22 File templateFile = new File(templateFileName); 23 BufferedReader reader = null; 24 FileWriter outputFile = null; 25 try { 26 //读取templateFile中的数据,并保存在字符串tempStr中 27 reader = new BufferedReader(new FileReader(templateFile)); 28 while(null != (data=reader.readLine())){ 29 tempStr.append(data); 30 } 31 //获取template中的数据,并保存在tempStr数组中 32 int iLast = -1; 33 int count = 0; 34 int index = tempStr.indexOf("{"); 35 while (index > 0){ 36 tempData[count++] = tempStr.substring(iLast+1, index); 37 iLast = tempStr.indexOf("}", index+1); 38 index = tempStr.indexOf("{", iLast+1); 39 } 40 41 //用数组result保存合并后的结果 42 for(int i = 0; i < tempRec.length; i++){ 43 result[i] = tempData[0] + tempRec[i].getCustomerNumber() 44 + tempData[1] + tempRec[i].getCustomerName() 45 + tempData[2] + tempRec[i].getJiGou() 46 + tempData[3] + tempRec[i].getSex() 47 + tempData[4] + tempRec[i].getAccountNumber() 48 + tempData[5] + tempRec[i].getHappenTime() 49 + tempData[6] + tempRec[i].getAmount(); 50 } 51 52 File file = new File(outputFileName); 53 outputFile = new FileWriter(file); 54 for(int i = 0; i < result.length; i++){ 55 outputFile.write(result[i]); 56 outputFile.write("\r\n"); 57 outputFile.flush(); 58 } 59 } 60 catch (FileNotFoundException e) { 61 e.printStackTrace(); 62 } 63 catch (IOException e1) { 64 e1.printStackTrace(); 65 } 66 finally { 67 if (null != reader && null != outputFile){ 68 try{ 69 reader.close(); 70 outputFile.close(); 71 } 72 catch (Exception e){ 73 e.printStackTrace(); 74 } 75 } 76 } 77 } 78 79 public static void main(String[] args){ 80 /* 81 * 文件data.txt 82 #客户号 姓名 所属机构号 性别 帐号 发生时间 发生额 83 000001,刘恭亮,0000,1,4155990188888888,20060720200005,300.00 84 000201,白晓龙,0002,1,4155990199999999,20060720200005,500.00 85 000101,黄戈华,0012,1,4155990100000000,20060720200005,1000.50 86 000101,张 东,0012,1,4155990155555555,20060720200005,600.99 87 000301,关婧媛,0013,0,4155990111111111,20060722201005,5000.00 88 000001,刘恭亮,0000,1,4155990188888888,20060725200005,200.00 89 */ 90 91 /*文件template.tmpl 92 * 客户号为{客户号},该客户的名字是{姓名},所属机构为{所属机构号}, 93 * 性别是{性别},帐号为{帐号},发生时间是{发生时间},金额是{金额}. 94 */ 95 //提示信息 96 String usageMsg = "请在带参数的eclipse下按照格式输入:\r\n" 97 + "-m data.txt -v template.tmpl -o outputFileName.txt"; 98 //文件dataFile和templateFile的前缀 99 String tempPath = System.getProperty("user.dir") 100 + "\\src\\test\\util\\"; 101 String dataFileName = null; 102 String templateFileName = null; 103 //输出文件的位置 104 String outputFileName = "c:\\"; 105 106 //参数不符合要求,打印提示信息并退出程序 107 if(6 != args.length){ 108 System.out.println(usageMsg); 109 System.exit(1); 110 } 111 112 //从命令行传递的参数获取需要组合的文件名以及合并后保存的文件名 113 for(int i = 0; i < args.length; i++){ 114 if(args[i].equals("-m")||args[i].equals("-M")) 115 dataFileName = tempPath + args[i+1]; 116 else if(args[i].equals("-v")||args[i].equals("-V")) 117 templateFileName = tempPath + args[i+1]; 118 else if(args[i].equals("-o")||args[i].equals("-O")) 119 outputFileName += args[i+1]; 120 } 121 composeMessage(dataFileName,templateFileName,outputFileName); 122 } 123 }