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