// Filecopy.kt (c) Kari Laitinen // http://www.naturalprogramming.com // 2024-02-21 File created. /* Compilation and execution: c:\freeware\kotlinc\bin\kotlinc Filecopy.kt -include-runtime -d RunThis.jar java -jar RunThis.jar */ import java.io.* fun main() { print( "\n This program copies text from one file" + "\n to another file. Please, give the name of" + "\n the file to be copied: " ) val name_of_file_to_be_copied = readLine()!! print( "\n Give the name of the duplicate file: " ) val name_of_new_duplicate_file = readLine()!! try { val file_to_be_copied = BufferedReader( FileReader( name_of_file_to_be_copied ) ) val new_duplicate_file = PrintWriter( FileWriter( name_of_new_duplicate_file ) ) print( "\n Copying in progress ... \n" ) var text_line_counter = 0 var end_of_input_file_encountered = false while ( end_of_input_file_encountered == false ) { val text_line_from_file = file_to_be_copied.readLine() if ( text_line_from_file == null ) { end_of_input_file_encountered = true } else { new_duplicate_file.println( text_line_from_file ) text_line_counter ++ } } file_to_be_copied.close() new_duplicate_file.close() print( "\n Copying ready. " + text_line_counter + " lines were copied. \n" ) } catch ( caught_file_not_found_exception : FileNotFoundException ) { print( "\n\n File \"" + name_of_file_to_be_copied + "\" not found.\n" ) } catch ( caught_io_exception : IOException ) { print( "\n\n File error. Probably cannot write to \"" + name_of_file_to_be_copied + "\".\n" ) } }