Docstoc

unit10key

Document Sample
unit10key Powered By Docstoc
					                                    1


                             Java Unit 10 Key


1.

import java.io.FileWriter;
import java.io.IOException;

public class Unit10Prog1
{
  public static void main(String[] args)
  {
    int i;
    char mychar;

     FileWriter mywriter = null;

     try
     {
       mywriter = new FileWriter("alphabet.txt");

         for(i = 65; i <= 90; i++)
         {
           mychar = (char) (i);
           mywriter.write(mychar);
           mychar = (char) (i + 32);
           mywriter.write(mychar);
         }
     }

    catch(IOException e)
    {
      System.out.println("Caught an IOException during opening
or writing" + e);
    }

    finally
    {
      try
      {
        if(mywriter != null)
          mywriter.close();
      }
      catch(IOException e)
      {
        System.out.println("Caught an IOException during
closing" + e);
                                         2


             }
         }

     }
}


2.

import java.io.FileReader;
import java.io.IOException;

public class Unit10Prog2
{
  public static void main(String[] args)
  {
    MyTerminalIO myterminal = new MyTerminalIO();
    int charasint;
    char charaschar;
    String charasstring;

         FileReader myreader = null;

         try
         {
           myreader = new FileReader("alphabet.txt");

             charasint = myreader.read();

             while(charasint != -1)
             {
               if((charasint) >= 97 && (charasint <= 122))
               {
                 charasint = charasint - 32;
                 charaschar = (char) charasint;
                 charasstring = "" + charaschar;
                 myterminal.print(charasstring);
               }
               else
               {
                 charaschar = (char) charasint;
                 charasstring = "" + charaschar;
                 myterminal.print(charasstring);
               }

                 charasint = myreader.read();
             }
                                   3


     }

    catch(IOException e)
    {
      System.out.println("Caught an IOException during opening
or reading" + e);
    }

     finally
     {

      try
      {
        if(myreader != null)
          myreader.close();
      }
      catch(IOException e)
      {
        System.out.println("Caught an IOException during
closing" + e);
      }
    }
  }
}


3.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Unit10Prog3
{
  public static void main(String[] args)
  {
    int charasint;
    char charaschar;
    String charasstring;

     FileReader myreader = null;
     FileWriter mywriter = null;

     try
     {
       myreader = new FileReader("alphabet.txt");
       mywriter = new FileWriter("alphachanged.txt");
                                   4




       charasint = myreader.read();

       while(charasint != -1)
       {
         if((charasint) >= 97 && (charasint <= 122))
         {
           charasint = charasint - 32;
           charaschar = (char) charasint;
           charasstring = "" + charaschar;
           mywriter.write(charasstring);
         }
         else
         {
           charaschar = (char) charasint;
           charasstring = "" + charaschar;
           mywriter.write(charasstring);
         }

           charasint = myreader.read();
       }
   }

    catch(IOException e)
    {
      System.out.println("Caught an IOException during opening,
reading, or writing" + e);
    }

   finally
   {

      try
      {
        if(myreader != null)
          myreader.close();
        if(mywriter != null)
          mywriter.close();
      }
      catch(IOException e)
      {
        System.out.println("Caught an IOException during
closing" + e);
      }
    }
  }
}
                                   5




4.

import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Unit10Prog4
{
  public static void main(String[] args)
  {
    int i;
    char mychar;
    String mystring;

     PrintWriter myprintwriter = null;
     FileWriter mywriter = null;

     String loopcontrol = "yes";
     String stringin = "";

     MyTerminalIO myterminal = new MyTerminalIO();

    String filename = myterminal.getString("Enter the name of
the file you'd like to write to.");

     try
     {
       mywriter = new FileWriter(filename);
       myprintwriter = new PrintWriter(mywriter);

      while(!loopcontrol.equalsIgnoreCase("no"))
      {
           stringin = myterminal.getString("Enter the string
you'd like added to the file.");
           myprintwriter.println(stringin);
           loopcontrol = myterminal.getString("Would you like to
add another string? (yes or no)");
         }
    }

    catch(IOException e)
    {
      System.out.println("Caught an IOException during opening
or writing" + e);
    }
                                6




    finally
    {
      try
      {
        if(mywriter != null)
          mywriter.close();
      }
      catch(IOException e)
      {
        System.out.println("Caught an IOException during
closing" + e);
      }
    }

    }
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:1/26/2012
language:
pages:6