// Fileput.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-09 File created. // 2005-05-09 Last modification. import java.util.* ; import java.io.* ; // Classes for file handling. import java.util.regex.* ; class Fileput { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program allows you to store text in a" + "\n text file. First give a file name: " ) ; String file_name_from_user = keyboard.nextLine() ; try { PrintWriter output_file = new PrintWriter( new FileWriter( file_name_from_user ) ) ; System.out.print( "\n Please, start typing in text from the keyboard." + "\n Type in Ctrl-A and Enter to finish. \n\n" ) ; // The Pattern.DOTALL flag specifies that also line-termination // characters are matched by the regular expression "." Pattern match_all_characters = Pattern.compile( ".", Pattern.DOTALL ) ; // We'll read one character at a time from the keyboard, but // but we must use a String object to store the single character. String character_from_keyboard = keyboard.findInLine( match_all_characters ) ; while( character_from_keyboard.charAt( 0 ) != '\u0001' ) { output_file.print( character_from_keyboard.charAt( 0 ) ) ; character_from_keyboard = keyboard.findInLine( match_all_characters ) ; } output_file.close() ; } catch ( Exception caught_exception ) { System.out.print( "\n Cannot write to file " + file_name_from_user ) ; } } }