Cut file Java (2 by 2) -
i started in java , galley little (a lot). explain problem, have flat file , read through java. goal create multiple output files (one file per order).
in file, example have order number (136670
, 136609
):
136670 00000000000000000 abc 136670 00000000000000000 abd 136670 00000000000000000 abe 136609 00000000000000000 abc 136609 00000000000000000 abd 136609 00000000000000000 abf 136609 00000000000000000 abe
so created hashmap
, or put order number 136670
, 136609
. 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 of code. use linkedhashmap
output file.
my code works not arrive 2 things necessary me:
if number of input values in
linkedhashmap
greater2
, want create newhashmap
,4
want newhashmap
.if number of input values in
linkedhashmap
greater2
, want increment figure of800
, if equal 4-801 on.
for example example file:
key: 136670
values:
136670 00000000000000000 abc 136670 00000000000000000 abd
with 800
.
key: 136670
values:
136670 00000000000000000 abe
with code 801
.
key: 136609
values:
136609 00000000000000000 abc 136609 00000000000000000 abd
with 800
.
key: 136609
values:
136609 00000000000000000 abe 136609 00000000000000000 abf
with code 801
.
key: 136609
values:
136609 00000000000000000 abe
with code 802
.
i thought go through hashmap
, linkedhashmap
treemap
can not cut hashmap
several subfiles ...
i tested way:
if (i % 2 == 0) { }
and list of hashmap: list<hashmap<string, stringbuilder>> maps = new arraylist<>();
doesn't work.
do have idea success division?
thank in advance.
i hope code serve example of need do
public static void main(string[] args) throws ioexception { bufferedreader br = new bufferedreader(new filereader("input.txt")); // map holds orders // each order, have list of orderitems // orderitems 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 dont have yet, lets create if (order == null) { order = new arraylist<>(); map.put(orderid, order); } list<string> orderitems; // if our order empty, no items @ all, create first group of items if (order.isempty()) { orderitems = new arraylist<>(); order.add(orderitems); } else { // otherwise last group of items orderitems = order.get(order.size()-1); } // if group, have 2 items, create new 1 if (orderitems.size() == 2) { orderitems = new arraylist<>(); order.add(orderitems); } // add item, group (orderitems) orderitems.add(line); } br.close(); // lets check if worked (string orderid: map.keyset()) { system.out.println("orderid: "+orderid); list<list<string>> order = map.get(orderid); int groupid = 800; (list<string> orderitems: order) { system.out.println(" group: "+groupid); (string item: orderitems) system.out.println(" "+item); groupid++; } } }
here's 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 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
Comments
Post a Comment