Java 8 Stream with batch processing

前端 未结 15 875
醉梦人生
醉梦人生 2020-11-28 02:42

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

15条回答
  •  温柔的废话
    2020-11-28 03:18

    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.

提交回复
热议问题