Compare two files to check equality in Kotlin
This post will discuss how to compare the contents of two files to check whether they are equal in Kotlin.
1. Using Files class
A simple solution is to read the entire file into a string and compare both strings for equality. The idea is to use the Files.readString() function to read all content from a file into a string, and then check the string’s equality using the contentEquals() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.File import java.nio.file.Files import java.nio.file.Path private fun isEqual(firstFile: Path, secondFile: Path): Boolean { if (Files.size(firstFile) != Files.size(secondFile)) { return false } val first = Files.readString(firstFile) val second = Files.readString(secondFile) return first!!.contentEquals(second) } fun main() { val firstFile = File("/files/first.txt") val secondFile = File("/files/second.txt") val isEqual = isEqual(firstFile.toPath(), secondFile.toPath()) if (isEqual) { println("Files are equal") } else { println("Files are not equal") } } |
Alternately, you can read all the bytes from the files into byte arrays and compare both byte arrays for equality. This can be done using the Files.readAllBytes() function, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.File import java.nio.file.Files import java.nio.file.Path private fun isEqual(firstFile: Path, secondFile: Path): Boolean { if (Files.size(firstFile) != Files.size(secondFile)) { return false } val first = Files.readAllBytes(firstFile) val second = Files.readAllBytes(secondFile) return first.contentEquals(second) } fun main() { val firstFile = File("/files/first.txt") val secondFile = File("/files/second.txt") val isEqual = isEqual(firstFile.toPath(), secondFile.toPath()) if (isEqual) { println("Files are equal") } else { println("Files are not equal") } } |
2. Using BufferedReader
Reading the entire contents of a file in a String or a byte array is not preferable for large files, since it can exhaust the heap memory. For large files, consider using the BufferedReader to read the files chunk-by-chunk.
The following solution demonstrates its usage by reading the file character-by-character with the BufferedReader#read() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.File import java.nio.file.Files import java.nio.file.Path private fun isEqual(firstFile: Path, secondFile: Path): Boolean { if (Files.size(firstFile) != Files.size(secondFile)) { return false } Files.newBufferedReader(firstFile).use { bf1 -> Files.newBufferedReader(secondFile).use { bf2 -> var ch: Int while (bf1.read().also { ch = it } != -1) { if (ch != bf2.read()) { return false } } } } return true } fun main() { val firstFile = File("/files/first.txt") val secondFile = File("/files/second.txt") val isEqual = isEqual(firstFile.toPath(), secondFile.toPath()) if (isEqual) { println("Files are equal") } else { println("Files are not equal") } } |
Alternately, you can read a file line-by-line with BufferedReader#readLine() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.io.File import java.nio.file.Files import java.nio.file.Path private fun isEqual(firstFile: Path, secondFile: Path): Boolean { if (Files.size(firstFile) != Files.size(secondFile)) { return false } Files.newBufferedReader(firstFile).use { bf1 -> Files.newBufferedReader(secondFile).use { bf2 -> var line: String while (bf1.readLine().also { line = it } != null) { if (line !== bf2.readLine()) { return false } } } } return true } fun main() { val firstFile = File("/files/first.txt") val secondFile = File("/files/second.txt") val isEqual = isEqual(firstFile.toPath(), secondFile.toPath()) if (isEqual) { println("Files are equal") } else { println("Files are not equal") } } |
That’s all about comparing the contents of two files for determining equality in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)