Skip to content

Instantly share code, notes, and snippets.

@rrguntaka
Created August 17, 2012 15:09
Show Gist options
  • Save rrguntaka/3379697 to your computer and use it in GitHub Desktop.
Save rrguntaka/3379697 to your computer and use it in GitHub Desktop.
Parse a text file and process batches
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaperfdemo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TextFileParser {
static int BATCH_SZIE = 10;
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fstream = new FileInputStream("newfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
List<List<String>> l = new ArrayList<List<String>>();
int counter = 0;
while ((strLine = br.readLine()) != null) {
l.add(Arrays.asList(strLine.split(",")));
counter++;
if (counter > BATCH_SZIE) {
process(l);
System.out.println("====================");
counter = 0;
l = new ArrayList<List<String>>();
}
}
System.out.println("Processing last batch");
process(l);
}
public static void process(List<List<String>> list) {
System.out.println(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment