Task

Remove a specific line or a number of lines from a file.

This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed).

For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2

Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed.

An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.




// version 1.1.2
 
import java.io.File
 
fun removeLines(fileName: String, startLine: Int, numLines: Int) {
    require(!fileName.isEmpty() && startLine >= 1 && numLines >= 1)
    val f = File(fileName)
    if (!f.exists()) {
        println("$fileName does not exist")
        return
    }
    var lines = f.readLines()
    val size = lines.size
    if (startLine > size) {
        println("The starting line is beyond the length of the file")
        return
    }
    var n = numLines
    if (startLine + numLines - 1 > size) {
        println("Attempting to remove some lines which are beyond the end of the file")
        n = size - startLine + 1
    }
    lines = lines.take(startLine - 1) + lines.drop(startLine + n - 1)
    val text = lines.joinToString(System.lineSeparator())
    f.writeText(text)
}
 
fun printFile(fileName: String, message: String) {
    require(!fileName.isEmpty())
    val f = File(fileName)
    if (!f.exists()) {
        println("$fileName does not exist")
        return
    }
    println("\nContents of $fileName $message:\n")
    f.forEachLine { println(it) }
}
 
fun main(args: Array<String>) {
    printFile("input.txt", "before removal")
    removeLines("input.txt", 2, 3)
    printFile("input.txt", "after removal of 3 lines starting from the second")
}