Ok mates, here is my code. I've got a problem, because "records.csv" is a file which cointains moreless 20 millions line, each one made of 4 fields separated with a ','.
As you can understand from the code, i'd like to have 4 Arraylists, each of them with all the values of a different field. The method after a while stop working (i think because to 'add' an element to the list, java has a pointer that have to tread all the arraylist before).
I need to solve, but i don't know how.
Suggestions?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class RecordReader {
static ArrayList<String> id = new ArrayList <String> ();
static ArrayList<String> field1 = new ArrayList <String> ();
static ArrayList<String> field2 = new ArrayList <String> ();
static ArrayList<String> field3 = new ArrayList <String> ();
public static void Reader () {
try {
FileReader filein = new FileReader("Y:/datasets/records.csv");
String token="";
String flag = "id";
int index=0, next;
do {
next = filein.read();
if (next != -1) {
if (next !=',' && next !='\n')
token = token + next;
else if (next == ','){
if (flag.compareTo("id")==0) {id.add (index, token); flag = "field1";}
else if (flag.compareTo("field1")==0) {field1.add (index, token); token=""; flag = "field2";}
else if (flag.compareTo("field2")==0) {field2.add (index, token); token=""; flag = "field3";}
}
else if (next == '\n') {
if (flag.compareTo("field3")==0) {field3.add (index, token); token=""; flag = "id"; index++;}
}
char nextc = (char) next;
System.out.print(nextc);
}
} while (next!=-1);
filein.close();
}
catch (IOException e) { System.out.println ("ERRORE, birichino!"); }
}
}
I have to do it all in once, the file is 711000 bytes.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.nio.CharBuffer.wrap(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at RecordReader.Reader(RecordReader.java:42)
at prova.main(prova.java:26)