Cut file Java (2 by 2)

I started in Java and I galley a little (a lot). I explain my problem, I have a flat file and I read through Java. The goal is to create multiple output files (one file per order).

In a file, for example I have an order number (136670 and 136609):

136670 00000000000000000 ABC

136670 00000000000000000 ABD

136670 00000000000000000 ABE

136609 00000000000000000 ABC

136609 00000000000000000 ABD

136609 00000000000000000 ABF

136609 00000000000000000 ABE

So I created a HashMap, or I put the order number 136670 and 136609. My current HashMap:

Key: 136670

Values:

136670 00000000000000000 ABC

136670 00000000000000000 ABD

136670 00000000000000000 ABE

Key: 136609

Values:

136609 00000000000000000 ABC

136609 00000000000000000 ABD

136609 00000000000000000 ABE

136609 00000000000000000 ABF

136609 00000000000000000 ABE

Code:

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

I put most of the code. I use the LinkedHashMap to output the file.

My code works but I do not arrive to do two things are necessary to me:

  • If the number of input values in the LinkedHashMap is greater than 2, I want create a new HashMap, then 4 I want new HashMap.

  • If the number of input values in the LinkedHashMap is greater than 2, I want to increment a figure of 800, if equal to 4-801 so on.

For example with my example file:

Key: 136670

Values:

136670 00000000000000000 ABC

136670 00000000000000000 ABD

With the 800.


Key: 136670

Values:

136670 00000000000000000 ABE

With the code 801.


Key: 136609

Values:

136609 00000000000000000 ABC

136609 00000000000000000 ABD

With the 800.


Key: 136609

Values:

136609 00000000000000000 ABE

136609 00000000000000000 ABF

With the code 801.


Key: 136609

Values:

136609 00000000000000000 ABE

With the code 802.


I thought to go through a HashMap, a LinkedHashMap a TreeMap but I can not cut a HashMap into several subfiles …

I tested this way:

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

And i list of HashMap: List<HashMap<String, StringBuilder>> maps = new ArrayList<>(); But it doesn’t work.

Do you have an idea for success this division?

Thank you in advance.

Answer

I hope this code will serve as example of what you need to do

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++;
        }
    }
}

here’s my 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

here’s the output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *