ReadFirstNLines

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ReadFirstNLines {

/**
* @param args
*/
public static void main(String[] args) {
String inputFile = “C:\\inputfile.txt”;
System.out.println(“Reading input file ” + inputFile + ” … “);
Map errorMap = new HashMap<String, String>();
try {
readALineAtATime(inputFile, “100”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void readALineAtATime(String inputFile, String numberOfLines) throws FileNotFoundException, IOException {
System.out.println(“Inside readALineAtATime”);
int lineLimit = Integer.valueOf(numberOfLines);
int lineCount = 0;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line;
while ((line = br.readLine()) != null && lineCount < lineLimit) {
System.out.println(line);
lineCount++;
}
br.close();

}
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.