Vote count:
0
This is my first time ever reading a file in java, so I've tried to make it easier on myself by using guava-18.0.jar. The code below is a pair of methods meant meant to read a file with 30,000 characters and parse it into a representative data matrix where 1 = real data and 2 = missing data.
The thing is, the parsing works beautifully and it prints out a matrix (see image above code). However, when the rest of my code runs, I get a java heap space error. This makes absolutely no sense because I previously built this code to handle a Random Matrix. When I use a random 79 x 33 matrix (same size as parsed matrix), the program completes in 1700 milliseconds successfully.
Is this an efficiency problem, or do I just have a really nasty logic error? I should note that when I run my parsed file, my program almost seems to enter an infinite loop (but this may just be an effect of my data now not being missing at random ... each 400 saves an integer array of size 91). Both methods (parsing and random) return a List> to the main method which is run through another set of classes.
public static List<List<Integer>> rFile(String[] args){
if (args.length != 4) {
System.out.println("Format: FileName colStart colEnd colSkipped");
System.exit(0);
}
try {
File file = new File(args[0]);
List<String> lines = Files.readLines(file, Charsets.UTF_8);
List<List<Integer>> matrix = new ArrayList<List<Integer>>();
Construct.colEnd = Integer.parseInt(args[2]);
Construct.colStart = Integer.parseInt(args[1]);
String[] colSkipped = args[3].split(",");
Construct.colSkip = new int[colSkipped.length];
for (int x = 0; x < colSkipped.length; x++) {
Construct.colSkip[x] = Integer.parseInt(colSkipped[x]);
}
for (String line: lines) {
String[] tokens = line.split(",");
List<Integer> rows = new ArrayList<Integer>(colEnd - colStart + 1 - colSkip.length);
for (int x = 1; x <= tokens.length; x++) {
if (x >= colStart && x <= colEnd && contains(x, colSkip) == false) {
try {
Double.parseDouble(tokens[x - 1]);
} catch (NumberFormatException e3) {
break;
}
if (tokens[x - 1].equals("-999")) { //
rows.add(2);
} else {
rows.add(1);
}
}
}
if (rows.size() == colEnd - colStart + 1 - colSkip.length) {
matrix.add(rows);
}
}
System.out.println(matrix.size() + "\t" + matrix.get(0).size());
return matrix;
} catch (IOException e1) {
System.out.println("IOEXCEPTION!!");
System.exit(0);
} catch (NumberFormatException e2) {
System.out.println("NumberFormatException!!");
System.exit(0);
}
return null;
}
private static boolean contains(int a, int[] colSkip) {
boolean bluejay = false;
for (int skip : colSkip) {
if (a == skip) {
bluejay = true;
}
}
return bluejay;
}
Java Heap Space error after reading file
Aucun commentaire:
Enregistrer un commentaire