// Fileprint.kt (c) Kari Laitinen // http://www.naturalprogramming.com // 2024-02-21 File created. /* Compilation and execution: c:\freeware\kotlinc\bin\kotlinc Fileprint.kt -include-runtime -d RunThis.jar java -jar RunThis.jar filename.txt */ import java.io.* fun main( command_line_parameters : Array ) { if ( command_line_parameters.size == 1 ) { val file_name_from_user = command_line_parameters[ 0 ] try { val file_to_print = BufferedReader( FileReader( file_name_from_user ) ) var line_counter = 0 var end_of_file_encountered = false while ( end_of_file_encountered == false ) { val text_line_from_file = file_to_print.readLine() if ( text_line_from_file == null ) { end_of_file_encountered = true } else { print( text_line_from_file + "\n" ) line_counter ++ } } file_to_print.close() print( "\n " + line_counter + " lines printed." ) } catch ( caught_file_not_found_exception : FileNotFoundException ) { print( "\n Cannot open file " + file_name_from_user ) } catch ( caught_io_exception : IOException ) { print( "\n Error while processing file " + file_name_from_user ) } } else { print( "\n You have to command this program so that" + "\n you give the file name from command line \n") } }