剪切文件Java(2乘2)

时间:2016-09-07 14:39:22

标签: java

我从Java开始,而且我有点(很多)厨房。 我解释了我的问题,我有一个平面文件,我通过Java阅读。 目标是创建多个输出文件(每个订单一个文件)。

在文件中,例如我有一个订单号(136670136609):

136670 00000000000000000 ABC

136670 00000000000000000 ABD

136670 00000000000000000 ABE

136609 00000000000000000 ABC

136609 00000000000000000 ABD

136609 00000000000000000 ABF

136609 00000000000000000 ABE

所以我创建了HashMap,或者我下了订单号136670136609。 我目前的HashMap

136670

136670 00000000000000000 ABC

136670 00000000000000000 ABD

136670 00000000000000000 ABE

136609

136609 00000000000000000 ABC

136609 00000000000000000 ABD

136609 00000000000000000 ABE

136609 00000000000000000 ABF

136609 00000000000000000 ABE

代码:

LinkedHashMap<String, StringBuilder> order = new LinkedHashMap<String, StringBuilder>();
while((line = br.readLine()) != null){
    String lineId = line.substring(ORDER_START_POSITION, ORDER_END_POSITION);
    if(order.get(lineId) != null){
        StringBuilder stringBuilder = order.get(lineId);
        stringBuilder.append("\n"+(line));
        order.put(lineId, stringBuilder);
    }
    else{
        order.put(lineId, new StringBuilder(line));
        }
}   

我把大部分代码都放了。我使用LinkedHashMap输出文件。

我的代码有效,但我不会做两件事对我来说是必要的:

  • 如果LinkedHashMap中的输入值大于2,我想创建一个新的HashMap,然后4我想要新HashMap 1}}。

  • 如果LinkedHashMap中的输入值大于2,我想增加一个800的数字,如果等于4-801那么。

例如我的示例文件:

136670

136670 00000000000000000 ABC

136670 00000000000000000 ABD

使用800

136670

136670 00000000000000000 ABE

使用代码801

136609

136609 00000000000000000 ABC

136609 00000000000000000 ABD

使用800

136609

136609 00000000000000000 ABE

136609 00000000000000000 ABF

使用代码801

136609

136609 00000000000000000 ABE

使用代码802

我想通过HashMapLinkedHashMapTreeMap,但我无法将HashMap切成几个子文件...

我测试过这种方式:

if (i % 2 == 0) {
    my
}

我的HashMap列表: List<HashMap<String, StringBuilder>> maps = new ArrayList<>(); 但它不起作用。

你对这个部门有成功的想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我希望此代码可以作为您需要做的事情的示例

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));

    // this map holds all orders
    // for each order, it will have a List of orderItems
    // orderItems is a List of String
    Map<String, List<List<String>>> map = new LinkedHashMap<>();
    String line;
    while((line = br.readLine()) != null){
        String orderId = line.substring(0, 6);

        // find order
        List<List<String>> order = map.get(orderId);
        // if we dont have it yet, lets create it
        if (order == null) {
            order = new ArrayList<>();
            map.put(orderId, order);
        }

        List<String> orderItems;
        // if our order is empty, no items at all, create the first group of items
        if (order.isEmpty()) {
            orderItems = new ArrayList<>();
            order.add(orderItems);
        } else {
            // otherwise we get the last group of items
            orderItems = order.get(order.size()-1);
        }

        // if this group, already have 2 items, we create a new one
        if (orderItems.size() == 2) {
            orderItems = new ArrayList<>();
            order.add(orderItems);
        }

        // finally we add the item, to the group (orderItems)
        orderItems.add(line);
    }   
    br.close();

    // now lets check if it worked

    for (String orderId: map.keySet()) {
        System.out.println("OrderId: "+orderId);
        List<List<String>> order = map.get(orderId);
        int groupId = 800;
        for (List<String> orderItems: order) {
            System.out.println("  Group: "+groupId);
            for (String item: orderItems)
                System.out.println("    "+item);
            groupId++;
        }
    }
}

这是我的input.txt

136670 00000000000000000 ABC
136670 00000000000000000 ABD
136670 00000000000000000 ABE
136609 00000000000000000 ABC
136609 00000000000000000 ABD
136609 00000000000000000 ABE
136609 00000000000000000 ABF
136609 00000000000000000 ABE

这是输出:

OrderId: 136670
  Group: 800
    136670 00000000000000000 ABC
    136670 00000000000000000 ABD
  Group: 801
    136670 00000000000000000 ABE
OrderId: 136609
  Group: 800
    136609 00000000000000000 ABC
    136609 00000000000000000 ABD
  Group: 801
    136609 00000000000000000 ABE
    136609 00000000000000000 ABF
  Group: 802
    136609 00000000000000000 ABE