Is there an option “go to line” in TextReader/StreamReader?

前端 未结 5 741
夕颜
夕颜 2021-01-18 01:16

I have a huge text file with 25k lines.Inside that text file each line starts with \"1 \\t (linenumber)\"

Example:

1   1   ITEM_ETC_GOLD_01    골드(소)          


        
5条回答
  •  不思量自难忘°
    2021-01-18 01:44

    If you are going to be looking up a lot of different lines from the file (but not all), then you may get some benefit from building an index as you go. Use any of the suggestions that are already here, but as you go along build up an array of byte-offsets for any lines that you have already located so that you can save yourself from re-scanning the file from the beginning each time.

    ADDENDUM:
    There is one more way you can do it fast if you only need the occasional 'random' line, but at the cost of a more complicated search (If Jon's answer is fast enough, I'd definitely stick with that for simplicity's sake).

    You could do a 'binary search', by just starting looking halfway down the file for the sequence '1', the first occurrence you find will give you an idea what line number you have found; then based on where the line you are looking for is relative to the found number you keep splitting recursively.

    For extra performance you could also make the assumption that the lines are roughly the same length and have the algorithm 'guess' the approximate position of the line you are looking for relative to the total number of lines in the file and then perform this search from there onwards. If you do not want to make assumptions about the length of the file you can even make it self-prime by just splitting in half first, and using the line number it finds first as an approximation of how many lines there are in the file as a whole.

    Definitely not trivial to implement, but if you have a lot of random access in files with a large number of lines, it may pay off in performance gains.

提交回复
热议问题