求助一个题目

xiongyu2006 2011-11-11 02:49:18
网上找的一个题:
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

---------------------------------------------------------------------------
我打算用map存放这两个文件的数据(data.txt,template.tmpl),Map<String,List<String>>=new HashMap<String,List<String>>,其中key就是表头的数据,list就是每一项的数据。现在的问题是,我怎么把读到一个一维数组的数据,按照数据类型存放到一个list数组中?
...全文
172 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiongyu2006 2011-11-14
  • 打赏
  • 举报
回复

package com.study.pratice02;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Test {
private String customerInfo;

public Test(String customerInfo) {
super();
this.customerInfo = customerInfo;
}

public String getCustomerInfo() {
return customerInfo;
}

public void setCustomerInfo(String customerInfo) {
this.customerInfo = customerInfo;
}

@Override
public String toString() {
return this.getCustomerInfo() + "\n";
}

}

public class TestMessage {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(composeMessage("c:" + File.separator + "data.txt",
"c:" + File.separator + "template.tmpl"));
}

public static String composeMessage(String dataFileName,
String templateFileName) {
File file1 = new File(templateFileName);
File file2 = new File(dataFileName);
List<Test> list = new ArrayList<Test>();
StringBuffer bf = new StringBuffer();
BufferedReader br = null;
String tempStr = "";
try {
br = new BufferedReader(new FileReader(file1));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while ((tempStr = br.readLine()) != null) {
list.add(new Test(tempStr));
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br = new BufferedReader(new FileReader(file2));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while ((tempStr = br.readLine()) != null) {
list.add(new Test(tempStr));
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
Object[] o = list.toArray();
for (Object ot : o) {
bf.append(ot.toString());
}
return bf.toString();
}

}

xiongyu2006 2011-11-14
  • 打赏
  • 举报
回复

package com.study.pratice;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Message {
private String cuId;
private String cuName;
private String insNum;
private String cusSex;
private String account;
private String occTime;
private String amount;

public Message(String[] str, int n) {
if (n == 7) {
this.cuId = str[0];
this.cuName = str[1];
this.insNum = str[2];
this.cusSex = str[3];
this.account = str[4];
this.occTime = str[5];
this.amount = str[6];
}

}

public String getCuId() {
return cuId;
}

public void setCuId(String cuId) {
this.cuId = cuId;
}

public String getCuName() {
return cuName;
}

public void setCuName(String cuName) {
this.cuName = cuName;
}

public String getInsNum() {
return insNum;
}

public void setInsNum(String insNum) {
this.insNum = insNum;
}

public String getCusSex() {
return cusSex;
}

public void setCusSex(String cusSex) {
this.cusSex = cusSex;
}

public String getAccount() {
return account;
}

public void setAccount(String account) {
this.account = account;
}

public String getOccTime() {
return occTime;
}

public void setOccTime(String occTime) {
this.occTime = occTime;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

@Override
public String toString() {
return getCuId() + " " + getCuName() + " " + getInsNum() + " "
+ getCusSex() + " " + getAccount() + " " + getOccTime() + " "
+ getAmount();
}

}

public class ComposeFile {

public static String composeMessage(String dataFileName,
String templateFileName) {
File file1 = new File(dataFileName);
File file2 = new File(templateFileName);
List<Message> list = new ArrayList<Message>();
StringBuffer bf = new StringBuffer();
BufferedReader br = null;
String s = "";
if (file1.exists() && file2.exists()) {
try {
br = new BufferedReader(new FileReader(file2));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while ((s = br.readLine()) != null) {
String[] tempStr = s.split(" ");
list.add(new Message(tempStr, tempStr.length));
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br = new BufferedReader(new FileReader(file1));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while ((s = br.readLine()) != null) {
String[] tempStr = s.split("\\|");
list.add(new Message(tempStr, tempStr.length));

}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
for (Message me : list) {
bf.append(me.toString()).append("\n");
}
}
return bf.toString();
}

public static void main(String[] args) {

System.out.println(composeMessage("c:\\data.txt", "c:\\template.tmpl"));

}

}

yyx520aiy520 2011-11-11
  • 打赏
  • 举报
回复
我不会..
xiongyu2006 2011-11-11
  • 打赏
  • 举报
回复
呵呵,明白了。
为什么我的思路这么不开阔呢?我只想着怎么给读到的数据进行分类
myhaikuotiankong 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 no9988 的回复:]
list 支持泛型的呀(list<?>)。你自定一个类型,循环数组add进去不就可以了。。
[/Quote]++
24K純帥 2011-11-11
  • 打赏
  • 举报
回复
把这些写到一个实体类中,再list<实体类>
no9988 2011-11-11
  • 打赏
  • 举报
回复
list 支持泛型的呀(list<?>)。你自定一个类型,循环数组add进去不就可以了。。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧