예제 #1
0
 public static String readFile(String filename) {
   String s = "";
   FileInputStream in = null;
   // FileReader in = null;
   try {
     File file = new File(filename);
     byte[] buffer = new byte[(int) file.length()];
     // char[] buffer = new char[(int) file.length()];
     in = new FileInputStream(file);
     // in = new FileReader(file);
     in.read(buffer);
     s = new String(buffer);
     in.close();
   } catch (FileNotFoundException fnfx) {
     System.err.println("File not found: " + fnfx);
   } catch (IOException iox) {
     System.err.println("I/O problems: " + iox);
   } finally {
     if (in != null) {
       try {
         in.close();
       } catch (IOException ignore) {
         // ignore
       }
     }
   }
   return s;
 }
예제 #2
0
  private static String slowStreamCopy(String filename) {
    String s = "";

    FileReader in = null;
    try {
      File file = new File(filename);

      int size = (int) file.length();

      char[] buffer = new char[size];
      in = new FileReader(file);

      // int count = in.read(buffer, 0, size);
      // if (count != -1)

      int count;
      while ((count = in.read(buffer, 0, size)) >= 0) {
        s = new String(buffer, 0, count);
      }
      in.close();
    }
    /*
     * String line; BufferedReader in = null; try { File file = new
     * File(filename); int size = (int)file.length(); if (size > 0) {
     * StringBuffer sb = new StringBuffer(size); in = new BufferedReader(new
     * InputStreamReader(new FileInputStream(filename),"ISO-8859-1")); while
     * ((line = in.readLine()) != null) { sb.append(line); } in.close(); s =
     * sb.toString(); } }
     */
    catch (FileNotFoundException fnfx) {
      System.err.println("File not found: " + fnfx);
    } catch (IOException iox) {
      System.err.println("I/O problems: " + iox);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ignore) {
          // ignore
        }
      }
    }
    return s;
  }