// StringLiteralsFromSourceFile.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-05-16 File created. // 2015-01-02 Last modification. import java.io.* ; import java.util.* ; import java.util.regex.Matcher; import java.util.regex.Pattern; class StringLiteralsFromSourceFile { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; String file_to_process = "Testfile.java" ; System.out.print( "\n The following was found in " + file_to_process + " \n" ) ; Pattern string_literals_pattern = Pattern.compile( "\"[^\"]*\"" ) ; // manages only simple string literals try { BufferedReader buffered_reader = new BufferedReader( new FileReader( file_to_process ) ) ; int line_counter = 0 ; boolean end_of_file_encountered = false ; while ( end_of_file_encountered == false ) { String text_line_from_file = buffered_reader.readLine() ; if ( text_line_from_file == null ) { end_of_file_encountered = true ; } else { line_counter ++ ; Matcher string_literals_matcher = string_literals_pattern.matcher( text_line_from_file ) ; while ( string_literals_matcher.find() ) { System.out.print( "\n " + string_literals_matcher.group() ) ; } } } buffered_reader.close() ; System.out.print( "\n " + line_counter + " lines processed.\n" ) ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n \"" + file_to_process + "\" not found.\n" ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n\n File reading error. \n" ) ; } } }