I have a very huge csv file and I have to use some select query, getting avg,... I can not do that normally by reading line by line, because of out of memory.
the following code work well on a short csv file but not for huge one. I will appreciate if you can edit this code to use for large csv file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Mu {
public void Computemu()
{
String filename="testdata.csv";
File file=new File(filename);
try {
Scanner inputstream=new Scanner(file);//Scanner read only string
// String data=inputstream.next();//Ignore the first line(header)
double sum=0;
double numberOfRating=0;
while (inputstream.hasNext())
{
String data=inputstream.next();//get a whole line
String[] values= data.split(";");//values separate by;
double rating=Double.parseDouble(values[2].replaceAll("\"", ""));//change value to string
if(rating>0)//do not consider implicit ratings
{
sum+=rating;
numberOfRating++;
}
}
inputstream.close();
System.out.println("Mu is"+ (sum/numberOfRating));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}