I'm trying to write a program that filters through data. The data contains 27,000 lines and is over 150mb. No matter how I try to implement the function, it stops printing prematurely around line 4,300. I've tested the loop without printing data (just printing the line number) and it reaches the full 27,000 lines. I'm thinking this might be a memory issue, but since i'm so new at Java, I'm not particularly sure where the problem might be. The two main suspects right now are line.substring and the PrintStream classes. Please help!
public static void main(String[] args) {
// tries to print output to output.csv in same directory
try {
PrintStream out = new PrintStream(new FileOutputStream("output.csv"));
System.setOut(out);
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
// read input file
File inputFile = new File("my-large-file.txt");
if(!inputFile.canRead()) {
System.out.println("Required input file not found; exiting.");
System.exit(1);
}
// doesn't allow me to use scanner without try for some reason
try {
Scanner input = new Scanner(inputFile);
while (input.hasNextLine()) {
String line = input.nextLine();
// scan through each line
Scanner lineScan = new Scanner(line);
// if we find the line that we want to look through
if(lineScan.next().startsWith("1")) {
// prints the specific data to output
String a= line.substring(007, 666);
if (!(a== "the-number-that-I-don't-want")) {
String current = line.substring(1, 10);
String another = line.substring(10, 20).replaceAll("\\s+","");
String third = line.substring(20, 30).replaceAll("\\s +","");
String fourth = line.substring(40, 50);
...
String nth = line.substring(999, 1000);
System.out.print(current + ", ");
System.out.print(another + ", ");
System.out.print(third + ", ");
System.out.print(fourth + ", ");
...
System.out.print(nth);
System.out.println();
}
}
}
}
catch(IOException e) {
e.printStackTrace();
}
}