I'm doing some tasks on CodeEval. Basically the task is very simple: "Print out the sum of all the integers read from the file".
My solution is following:
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
public class SumIntegersFromFile {
public static void main(String args[]) throws IOException{
File file = new File(args[0]);
BufferedReader br = new BufferedReader( new FileReader(file));
String line;
int i=0;
while((line=br.readLine())!=null){
int k = Integer.parseInt(line);
i+=k;
}
br.close();
System.out.println(i);
}
}
But I was told this solution is not optimal from a performance point of view.
The code is based on the recommendations in the question Best way to read a text file. The only difference here is I am reading integers instead of strings.
What is the most performance-efficient way to read integers from a file in Java?