Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Mark a String in a File?

Tags:

java

I have a text file. It is designed as following:

#1{1,12,345,867}
#2{123, 3243534, 2132131231} 
#3{234, 35345}
#4{}

... (at the end of an each entry stands "\n")

That is an example. In fact my strings #number{number,number,...,number} could be really long...

Here is a template of a constructor of a class which works with this file:

public Submatrix(String matrixFilePath, int startPos, int endPos) throws FileNotFoundException{

}

As you can see submatrix is determined by startPos and endPos numbers of strings of a matrix.

My question is : "How could I count strings to reach the right one?" My file can contain billions of strings. Should I use LineNumberReader->readLine() billions times?????

like image 827
Dmitry Avatar asked Mar 17 '26 14:03

Dmitry


1 Answers

I would be tempted to read each line sequentially until I reached the desired line. However, since the lines are numbered in the file and delimited with newlines you can treat the file as random access and employ various strategies. For example, you get use a variant of binary search to quickly find the starting line. You can estimate the average line length from the first N lines and then try to make a more accurate guess as to the starting location, and so on.

like image 85
President James K. Polk Avatar answered Mar 20 '26 11:03

President James K. Polk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!