如何从一个文件夹中读取两个文件?

时间:2015-06-02 11:01:26

标签: java

下面给出的代码有两个不同的输入,但我想只传递单个输入,即文件夹的路径" test"其余的功能原样。 而且final.tbl是生成它应该在相同的输入文件夹路径中生成:

public class Migrator {
private static final String KEY1 = "post_tran_id";
private static final String KEY2 = "post_tran_cust_id";

void migrate(String post_tran, String post_tran_cust) throws IOException {
    Map<String, Map<String, String>> h1 = loadFile(post_tran, KEY1);
    Map<String, Map<String, String>> h2 = loadFile(post_tran_cust, KEY2);

    PrintStream out = new PrintStream("final.tbl");

    for (Map.Entry<String, Map<String, String>> entry : h1.entrySet()) {
        Map<String, String> data = entry.getValue();
        String k = data.get(KEY2);
        if (k != null && h2.containsKey(k)) {
            print(out, KEY1, data.get(KEY1));
            print(out, KEY2, data.get(KEY2));
            // Print remaining rows in any order
            for (String key : data.keySet()) {
                if ( ! key.equals(KEY1) && ! key.equals(KEY2) ) {
                    print(out, key, data.get(key));
                }
            }
            data = h2.get(k);
            for (String key : data.keySet()) {
                if ( ! key.equals(KEY2) ) {
                    print(out, key, data.get(key));
                }
            }
            out.println(); // Record separator
        }
    }
 }

 private void print(PrintStream out, String key, String data) {
    out.print("[name]");
    out.print(key);
    out.print("[/name]");
    out.print("=");
    out.print("[data]");
    out.print(data);
    out.print("[/data]");
    out.println();
 }

private Map<String, Map<String, String>> loadFile(String fileName, String key) throws 
 IOException {
    Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>> 
 ();
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String line;

    do {
        Map<String, String> data = new HashMap<String, String>();

        while ((line = br.readLine()) != null && !line.isEmpty()) {
            data.put(getKey(line), getData(line));
        }

        result.put(data.get(key), data);
    } while (line != null);

    br.close();

    return result;
}

private String getKey(String line) {
    String[] tokens = line.split("=");
    int length = tokens[0].length();
    return tokens[0].substring(6, length - 7);
}

private String getData(String line) {
    String[] tokens = line.split("=");
    int length = tokens[1].length();
    return tokens[1].substring(6, length - 7);
}



 public static void main(String[] args) throws IOException { Migrator mg =
 new Migrator();

 mg.migrate("D:\\test\\post_tran.tbl",
 "D:\\test\\post_tran_cust.tbl"); }

 }

1 个答案:

答案 0 :(得分:0)

要使迁移方法采用1参数但能够使用多个路径,您始终可以将所有路径附加到一个字符串中并在迁移中解析它们方法


示例:

String appendedArgument = "D:\\test\\post_tran.tbl;D:\\test\\post_tran_cust.tbl";

注意分隔两个路径的分号。

然后你可以叫你方法:

mg.migrate(appendedArgument);

在另一边解析它:

void migrate(String argument) throws IOException
{
     String[] splitArgument.split(";");

     String post_tran = splitArgument[0];
     String post_tran_cust = splitArgument[1];

     Map<String, Map<String, String>> h1 = loadFile(post_tran, KEY1);
     Map<String, Map<String, String>> h2 = loadFile(post_tran_cust, KEY2);
}

使用这种方法,您可以根据需要向迁移方法发送尽可能多的路径,这使您(在此特定情况下)也可以发送要存储 final.tbl的路径文件。
这会使attachmentArgument字符串看起来像:

String appendedArgument = "D:\\test\\;D:\\test\\post_tran.tbl;D:\\test\\post_tran_cust.tbl";

然后您需要在迁移方法中对其进行相应的解析。