我从Java开始,我做了一些(很多)的工作。我解释我的问题,我有一个平面文件,我通过Java阅读。目标是创建多个输出文件(每个订单一个文件)。
例如,在一个文件中,我有一个订单号(136670和136609):
136670 00000000000000000 ABC
136670 00000000000000000 ABD
136670 00000000000000000 ABE
136609 00000000000000000 ABC
136609 00000000000000000 ABD
136609 00000000000000000 ABF
136609 00000000000000000 ABE所以我创建了一个HashMap,或者输入订单号136670和136609。我现在的HashMap
Key:136670
值:
136670 00000000000000000 ABC
136670 00000000000000000 ABD
136670 00000000000000000 ABEKey: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。LinkedHashMap中的输入值的数量大于2,我希望增加一个800的数字,如果等于4-801,依此类推。例如,使用我的示例文件:
Key:136670
值:
136670 00000000000000000 ABC
136670 00000000000000000 ABD和800一起。
Key:136670
值:
136670 00000000000000000 ABE使用代码801。
Key:136609
值:
136609 00000000000000000 ABC
136609 00000000000000000 ABD和800一起。
Key:136609
值:
136609 00000000000000000 ABE
136609 00000000000000000 ABF使用代码801。
Key:136609
值:
136609 00000000000000000 ABE使用代码802。
我想要通过一个HashMap,一个LinkedHashMap,一个TreeMap,但是我不能把一个HashMap分成几个子文件.
我是这样测试的:
if (i % 2 == 0) {
my
}我列出了HashMap:List<HashMap<String, StringBuilder>> maps = new ArrayList<>();,但它不起作用。
你对这个部门的成功有什么想法吗?
提前谢谢你。
发布于 2016-09-07 16:17:37
我希望这段代码可以作为你需要做的事情的例子。
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 ABEhttps://stackoverflow.com/questions/39372880
复制相似问题