网上找的一个题:
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数组中?

解决方案 »

  1.   

    list 支持泛型的呀(list<?>)。你自定一个类型,循环数组add进去不就可以了。。
      

  2.   

    把这些写到一个实体类中,再list<实体类>
      

  3.   


    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")); }}
      

  4.   


    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();
    }}