Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.io.*;
  2.  
  3. public class StringToFileWriterMain {
  4.     public static void main(String[] args) {
  5.         try {
  6.             // the buffered reader
  7.             BufferedReader myBufferedReader = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9.             // the string to write
  10.             System.out.println("Type string to write");
  11.             String stringToWrite = myBufferedReader.readLine();
  12.  
  13.             // the file to write to
  14.             System.out.println("Type file to write to");
  15.             String fileToWriteTo = myBufferedReader.readLine();
  16.  
  17.             ThingToThingWriter myThingToThingWriter = ThingToThingWriterFactory.createThingToThingWriter("String", "File");
  18.             myThingToThingWriter.writeThingToThing(stringToWrite, fileToWriteTo);
  19.         } catch(Exception e) {
  20.             System.out.println("ERROR OCCURRED");
  21.         }
  22.     }
  23. }
  24.  
  25. interface ThingToThingWriter {
  26.     public boolean writeThingToThing(String thing1, String thing2);
  27. }
  28.  
  29. class StringToFileWriter implements ThingToThingWriter {
  30.  
  31.     protected StringToFileWriter() {
  32.         // constructor
  33.     }
  34.  
  35.     /**
  36.      * Writes stringToWrite to fileToWriteTo.
  37.      * Returns true if stringToWrite is a long string.
  38.      */
  39.     public boolean writeThingToThing(String stringToWrite, String fileToWriteTo) {
  40.         try {
  41.             BufferedWriter myBufferedWriter = new BufferedWriter(new FileWriter(fileToWriteTo));
  42.             myBufferedWriter.write(stringToWrite);
  43.             myBufferedWriter.close();
  44.  
  45.             if(getLengthOfString(stringToWrite) > 17.0) {
  46.                 return true;
  47.             } else {
  48.                 return false;
  49.             }
  50.         } catch(Exception e) {
  51.             System.out.println("ERROR OCCURRED");
  52.             return false;
  53.         }
  54.     }
  55.  
  56.     // returns a double because the string might be too long for an integer
  57.     private double getLengthOfString(String stringToGetLengthOf) {
  58.         double lengthOfString = 0.0;
  59.         try {
  60.             while(true) {
  61.                 stringToGetLengthOf.charAt((int) lengthOfString);
  62.                 lengthOfString = lengthOfString + 1.0;
  63.             }
  64.         } catch(Exception e) {
  65.             return lengthOfString;
  66.         }
  67.     }
  68. }
  69.  
  70. class ThingToThingWriterFactory {
  71.     public static ThingToThingWriter createThingToThingWriter(String thing1Type, String thing2Type) {
  72.         if(thing1Type.equalsIgnoreCase("String") && thing2Type.equalsIgnoreCase("File")) {
  73.             return new StringToFileWriter();
  74.         } else {
  75.             throw new RuntimeException("WRITING THINGS OF TYPE "+thing1Type+" TO THINGS OF TYPE "+thing2Type+" IS NOT YET SUPPORTED");
  76.         }
  77.     }
  78. }