Read specific line from text file
in
Programming Questions
•
2 years ago
Hi all, I would like to pull specific lines from a very large text file - several gigs, so way too large to use loadStrings().
I have a version working that uses a BufferedReader and readLine() to pull every line from the file sequentially. Looking through the documentation, it appears that readLine() starts at line 0 and each time called increments which line it gets. Is this correct?
If I want to pull a specific line, say #100, is this possible?
Here's a piece of my code, where I'm reading from a text file of hex color values and filling every pixel with those values. I need specific lines so when I reach a certain number, the file is saved and the process is started again but at the next point in the text file.
I have a version working that uses a BufferedReader and readLine() to pull every line from the file sequentially. Looking through the documentation, it appears that readLine() starts at line 0 and each time called increments which line it gets. Is this correct?
If I want to pull a specific line, say #100, is this possible?
Here's a piece of my code, where I'm reading from a text file of hex color values and filling every pixel with those values. I need specific lines so when I reach a certain number, the file is saved and the process is started again but at the next point in the text file.
- // read hex values from the text file and fill pixels[] with those values
loadPixels();
numPx = pixels.length;
for (int i=0; i<numPx; i++) {
// read line from file
try {
hexLine = reader.readLine();
println(count + ": " + hexLine);
count++;
}
// if there's an issue, print the error and quit
catch (IOException e) {
e.printStackTrace();
hexLine = null;
println("Error reading file!");
exit();
}
}
2