Search code examples
countlinerascal

Rascal MPL get line count of file without loading its contents


Is there a more efficient way than

int fileSize = size(readFileLines(fileLoc));

to get the total number of lines in a file? I presume this code has to read the entire file first, which could become costly for huge files.

I have looked into IO and Loc whether some of this info might be saved in conjunction with the file.


Solution

  • This is the way, unless you'd like to call wc -l via util::ShellExec 😁

    Apart from streaming the file and saving some memory counting lines is always linear in the size of the file so you won't win much time.