I have a large file that contains a list of items.
I would like to create a batch of items, make an HTTP request with this batch (all of the items are needed as par
Simple example using Spliterator
// read file into stream, try-with-resources
try (Stream stream = Files.lines(Paths.get(fileName))) {
//skip header
Spliterator split = stream.skip(1).spliterator();
Chunker chunker = new Chunker();
while(true) {
boolean more = split.tryAdvance(chunker::doSomething);
if (!more) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class Chunker {
int ct = 0;
public void doSomething(T line) {
System.out.println(ct++ + " " + line.toString());
if (ct % 100 == 0) {
System.out.println("====================chunk=====================");
}
}
}
Bruce's answer is more comprehensive, but I was looking for something quick and dirty to process a bunch of files.