1 // inport needed classes from packages
  2 ÏÏÏimport java.util.Scanner;
  3 ÏÏÏimport java.io.*;
  4 
  5 ÏÕÖ×public class Lab6
  6 ÏÏ§{
  7 ÏÏ§ÏÞßàpublic static void main (String [] args)throws IOException
  8 ÏÏ§ÏÏ§{
  9 ÏÏ§ÏÏ§
 10 ÏÏ§ÏÏ§// declarations of needed variables and objects
 11 ÏÏ§ÏÏ¨¹íÏchar [ ] letters;                        // character array
 12 ÏÏ§ÏÏ¨¹íÏString inputString;                      // input String 
 13 ÏÏ§ÏÏ¨¹íÏScanner fileScanner, keyboardScanner;    // file and keyboard Scanners
 14 ÏÏ§ÏÏ¨¹íÏString inputFileName, outputFileName;    // input and output filenames
 15 ÏÏ§ÏÏ¨¹íÏboolean inputFileFound;                  // boolean telling whether file found
 16 ÏÏ§ÏÏ¨¹íÏint inputStringLength;                   // integer String length
 17 ÏÏ§ÏÏ¨¹íÏFileWriter myFileWriter;                 // FileWriter   
 18 ÏÏ§ÏÏ¨¹íÏBufferedWriter myBufferedWriter;         // BufferedWriter
 19 ÏÏ§ÏÏ¨¹íÏPrintWriter    myPrintWriter;            // Print Writer
 20 ÏÏ§ÏÏ§         
 21 ÏÏ§ÏÏ§// initialize needed variables
 22 ÏÏ§ÏÏ¨¹¹ÏinputFileFound = false;
 23 ÏÏ§ÏÏ§
 24 ÏÏ§ÏÏ§
 25 ÏÏ§ÏÏ§// instantiate keyboardScanner
 26 ÏÏ§ÏÏ¨¹¹ÏkeyboardScanner = new Scanner (System.in); 
 27 ÏÏ§ÏÏ§
 28 ÏÏ§ÏÏ§// get filenames from user
 29 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" please enter the name of your input data file ");
 30 ÏÏ§ÏÏ¨¹¹ÏinputFileName = keyboardScanner.nextLine();
 31 ÏÏ§ÏÏ§      
 32 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" please enter the name of your output data file ");
 33 ÏÏ§ÏÏ¨¹¹ÏoutputFileName = keyboardScanner.nextLine();
 34 ÏÏ§ÏÏ§
 35 ÏÏ§ÏÏ§// instantiate input file and keep trying until successful
 36 ÏÏ§ÏÏ¨¹¹®do
 37 ÏÏ§ÏÏ§ÏÏ7¹¹´try
 38 ÏÏ§ÏÏ§ÏÏ5ÏÏ§{
 39 ÏÏ§ÏÏ§ÏÏ5ÏÏ¨¹¹ÏfileScanner = new Scanner (new File (inputFileName));
 40 ÏÏ§ÏÏ§ÏÏ5ÏÏ¨¹¹ÏinputFileFound = true;
 41 ÏÏ§ÏÏ§ÏÏ5ÏÏ§}
 42 ÏÏ§ÏÏ§ÏÏ5ÏÏ§Ïðîìcatch (FileNotFoundException fnfe)
 43 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§{
 44 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ¨¹¹ÏfileScanner = new Scanner (new File ("squib.txt"));
 45 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" your file could not be found ");
 46 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" please enter another name ");
 47 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ¨¹¹ÏinputFileName = keyboardScanner.nextLine();
 48 ÏÏ§ÏÏ§ÏÏ5ÏÏ©ÏÏ©}
 49 ÏÏ§ÏÏ§ÏÏòwhile (!inputFileFound);
 50 ÏÏ§ÏÏ§
 51 ÏÏ§ÏÏ§   // get data from file
 52 ÏÏ§ÏÏ¨¹¹ÏinputString = fileScanner.nextLine();
 53 ÏÏ§ÏÏ§
 54 ÏÏ§ÏÏ§// instantiate Objects needed to output to a  file    
 55 ÏÏ§ÏÏ¨¹¹ÏmyFileWriter = new FileWriter (outputFileName);
 56 ÏÏ§ÏÏ¨¹¹ÏmyBufferedWriter = new BufferedWriter (myFileWriter);
 57 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter = new PrintWriter (myBufferedWriter);
 58 ÏÏ§ÏÏ§      
 59 ÏÏ§ÏÏ§// echo the input String to the screen & to your output file
 60 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" the input String was:    " + inputString);
 61 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.println (" the input String was:   " + inputString);
 62 ÏÏ§ÏÏ§
 63 ÏÏ§ÏÏ§//  determine input String's length & output it to screen & output file
 64 ÏÏ§ÏÏ¨¹¹ÏinputStringLength = inputString.length();
 65 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println 
 66 ÏÏ§ÏÏ§ÏÏÏÏÏÏ(" the length of the input string is " + inputStringLength);
 67 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.println 
 68 ÏÏ§ÏÏ§ÏÏÏÏÏÏ(" the length of the input string is " + inputStringLength); 
 69 ÏÏ§ÏÏ§
 70 ÏÏ§ÏÏ§/*  
 71 ÏÏ§ÏÏ§    output the characters in the input String to file & screen, 1 per line
 72 ÏÏ§ÏÏ§    preeceded by the following statement: 
 73 ÏÏ§ÏÏ§    Here are the input file characters printed using a standard FOR statement 
 74 ÏÏ§ÏÏ§*/
 75 ÏÏ§ÏÏ§  
 76 ÏÏ§ÏÏ§     // print the header statements
 77 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println 
 78 ÏÏ§ÏÏ§ÏÏÏÏÏÏ("  Here are the input file characters printed using a standard FOR statement ");
 79 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.println
 80 ÏÏ§ÏÏ§ÏÏÏÏÏÏ("  Here are the input file characters printed using a standard FOR statement ");
 81 ÏÏ§ÏÏ§
 82 ÏÏ§ÏÏ§    // print the characters in the String
 83 ÏÏ§ÏÏ¨¹¹±for (int i = 0; i < inputStringLength; i++)
 84 ÏÏ§ÏÏ§ÏÏ5{
 85 ÏÏ§ÏÏ§ÏÏ7¹¹ÏSystem.out.println (inputString.charAt(i));
 86 ÏÏ§ÏÏ§ÏÏ7¹¹ÏmyPrintWriter.println (inputString.charAt(i));
 87 ÏÏ§ÏÏ§ÏÏ°}
 88 ÏÏ§ÏÏ§
 89 ÏÏ§ÏÏ§// print a blank line to the screen and to the file
 90 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println ();
 91 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.println ();
 92 ÏÏ§ÏÏ§
 93 ÏÏ§ÏÏ§// transform the inputString into an array using the String Method
 94 ÏÏ§ÏÏ¨¹¹Ïletters = inputString.toCharArray();
 95 ÏÏ§ÏÏ§
 96 ÏÏ§ÏÏ§/* using the alternate form of a FOR loop described on pages 249, shown on page 251 and
 97 ÏÏ§ÏÏ§   on page 384  print the letters in the array on a single line
 98 ÏÏ§ÏÏ§   preceded by the following statement
 99 ÏÏ§ÏÏ§   Here are the character in the array printed using the foreach loop form
100 ÏÏ§ÏÏ§*/
101 ÏÏ§ÏÏ§
102 ÏÏ§ÏÏ§   // print the header statements   
103 ÏÏ§ÏÏ¨¹¹ÏSystem.out.print 
104 ÏÏ§ÏÏ§ÏÏÏÏÏÏ("  Here are the input file characters printed using a foreach statement:  ");
105 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.print
106 ÏÏ§ÏÏ§ÏÏÏÏÏÏ("  Here are the input file characters printed using a foreach statement: ");
107 ÏÏ§ÏÏ§
108 ÏÏ§ÏÏ§   // print the characters in the array using the foreach loop form
109 ÏÏ§ÏÏ¨¹¹±for ( char theLetters : letters)
110 ÏÏ§ÏÏ§ÏÏ5{
111 ÏÏ§ÏÏ§ÏÏ7¹¹ÏSystem.out.print (theLetters);
112 ÏÏ§ÏÏ§ÏÏ7¹¹ÏmyPrintWriter.print (theLetters);
113 ÏÏ§ÏÏ§ÏÏ°}
114 ÏÏ§ÏÏ§
115 ÏÏ§ÏÏ§// print two blank lines
116 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println();
117 ÏÏ§ÏÏ§
118 ÏÏ§ÏÏ§// close the output file
119 ÏÏ§ÏÏ¨¹¹ÏmyPrintWriter.close();
120 ÏÏ§ÏÏ§
121 ÏÏ§ÏÏ§// print out the names of the input and output files
122 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println ( " The input file used was : " + inputFileName);
123 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println ( " The output file created was : " + outputFileName);
124 ÏÏ§ÏÏ©}
125 ÏÏ©}
126     
127