Example #1
0
  // Read the file into a String
  public static String readFileToString(String filename) {
    try {
      File file = new File(filename);
      if (!file.exists()) {
        System.out.println("\nFILE DOES NOT EXIST: " + filename);
      }
      BufferedReader in = new BufferedReader(new FileReader(file));

      // Create an array of characters the size of the file
      char[] allChars = new char[(int) file.length()];

      // Read the characters into the allChars array
      in.read(allChars, 0, (int) file.length());
      in.close();

      // Convert to a string
      String allCharsString = new String(allChars);

      return allCharsString;

    } catch (FileNotFoundException e) {
      System.err.println(e);
      return "";
    } catch (IOException e) {
      System.err.println(e);
      return "";
    }
  }
Example #2
0
 /**
  * Copy a file from a source directory to a destination directory (if it is not there already). If
  * <code>overwrite</code> is true and the destination file already exists, overwrite it.
  *
  * @param configuration Holds the error message
  * @param file The name of the file to copy
  * @param source The source directory
  * @param destination The destination directory where the file needs to be copied
  * @param overwrite A flag to indicate whether the file in the destination directory will be
  *     overwritten if it already exists.
  * @param replaceNewLine true if the newline needs to be replaced with platform- specific newline.
  */
 public static void copyFile(
     Configuration configuration,
     String file,
     String source,
     String destination,
     boolean overwrite,
     boolean replaceNewLine) {
   DirectoryManager.createDirectory(configuration, destination);
   File destfile = new File(destination, file);
   if (destfile.exists() && (!overwrite)) return;
   try {
     InputStream in =
         Configuration.class.getResourceAsStream(
             source + DirectoryManager.URL_FILE_SEPARATOR + file);
     if (in == null) return;
     OutputStream out = new FileOutputStream(destfile);
     try {
       if (!replaceNewLine) {
         byte[] buf = new byte[2048];
         int n;
         while ((n = in.read(buf)) > 0) out.write(buf, 0, n);
       } else {
         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
         try {
           String line;
           while ((line = reader.readLine()) != null) {
             writer.write(line);
             writer.write(DocletConstants.NL);
           }
         } finally {
           reader.close();
           writer.close();
         }
       }
     } finally {
       in.close();
       out.close();
     }
   } catch (IOException ie) {
     ie.printStackTrace(System.err);
     throw new DocletAbortException();
   }
 }