1

I need to read from 2 files and compare their content line by line. So I need something like file.readNextLine() functionality. How can I achieve that in Kotlin?

Edit: Both files are already sorted. Some lines are absent in first file, some lines are absent in second file. I need to find this difference.

gs_vlad
  • 1,409
  • 4
  • 15
  • 29
  • @Roland I edited question – gs_vlad Mar 15 '19 at 12:20
  • thanks for your update. If you wouldn't have written that you are looking for differences I would have suggested you to use `zip` here... but now... did you try something already? – Roland Mar 15 '19 at 12:22

1 Answers1

1

Difference between files is a hard topic in general, but it depends on what kind of difference you'd like to spot. For example, a minimal difference? Or, maybe, it will be OK for you to report N-1 lines change if the only first line is missing? A diff tool solves that. You may check a related thread on that:
Diff Algorithm?


    File("a").useLines { a ->
      File("b").useLines { b -> 

        val aIt = a.iterator()
        val bIt = b.iterator()

        //Do the DIFF on iterators
      }
    } 

The code reads files in Kotlin in a line-by-line lazy fashion (using sequences). Next, you may use iterators to implement the diff algorithm.

Eugene Petrenko
  • 4,874
  • 27
  • 36