// Filecopy.java (c) 1998-2005 Kari Laitinen // http://www.naturalprogramming.com // 2003-09-12 File created. // 2005-05-08 Last modification. // In the javafilesextra folder there is a program named // FilecopyBetter.java which is an enhanced version of this program. import java.util.* ; import java.io.* ; // Classes for file handling. class Filecopy { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program copies text from one file" + "\n to another file. Please, give the name of" + "\n the file to be copied: " ) ; String name_of_file_to_be_copied = keyboard.nextLine() ; System.out.print( "\n Give the name of the duplicate file: " ) ; String name_of_new_duplicate_file = keyboard.nextLine() ; try { BufferedReader file_to_be_copied = new BufferedReader( new FileReader( name_of_file_to_be_copied ) ) ; PrintWriter new_duplicate_file = new PrintWriter( new FileWriter( name_of_new_duplicate_file ) ) ; System.out.print( "\n Copying in progress ... \n" ) ; int text_line_counter = 0 ; boolean end_of_input_file_encountered = false ; while ( end_of_input_file_encountered == false ) { String 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 ++ ; } } System.out.print( "\n Copying ready. " + text_line_counter + " lines were copied. \n" ) ; file_to_be_copied.close() ; new_duplicate_file.close() ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n\n File \"" + name_of_file_to_be_copied + "\" not found.\n" ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n\n File error. Probably cannot write to \"" + name_of_file_to_be_copied + "\".\n" ) ; } } }