My code picks a value from a too large csv file (~300MB.) at a given index i
and j
(assuming the csv as 2D array because it contains just data without headers), it works but it is slow.
Is there other method to pick a value from csv file faster?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
public class Main
{
public static String r1(String input,int i, int j,int bufferSize){
String[][] result = {};
String row ="";
try {
BufferedReader r1 = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input)), "UTF-8"), bufferSize);
List<String[]> lines = new ArrayList<String[]>();
while((row=r1.readLine())!=null){
lines.add(row.split(","));
}
String[][] csv = new String[lines.size()][0];
lines.toArray(csv);
result = csv;
r1.close();
}
catch(IOException e){
e.printStackTrace();
}
return result[i][j];
}
public static void main(String[] args){
int bufferSize = 10 * 1024; //10 KB.
String filename = "matrix.csv";
int i = 744, j = 7000;
String pickedValue = r1(filename,i,j,bufferSize);
System.out.println(pickedValue);
}
}