View Javadoc

1   /*
2    * Created on 27.7.2004
3    */
4   package net.sf.tomp.util.io;
5   
6   import java.io.File;
7   import java.io.FileReader;
8   import java.io.IOException;
9   import java.io.InputStreamReader;
10  import java.io.Reader;
11  import java.io.StringWriter;
12  
13  import java.net.URL;
14  
15  /***
16   * File utilities, such as getting whole file content as String. 
17   * @author tomp
18   */
19  public class FileUtil {
20  	
21      /***
22       * @param f file to be read
23       * @return whole file content
24       * @throws IOException
25       */
26      public static String getFileContent(File f) throws IOException {
27          FileReader rd = new FileReader(f);
28          StringWriter wr = new StringWriter();
29  
30          int oneChar;
31  
32          while ((oneChar = rd.read()) != -1) {
33              wr.write(oneChar);
34          }
35  
36          rd.close();
37          wr.close();
38  
39          return wr.toString();
40      }
41  
42      /***
43       * @param u file identified by URL
44       * @return file content
45       * @throws IOException
46       */
47      public static String getFileContent(URL u) throws IOException {
48          Reader rd = new InputStreamReader(u.openStream());
49          StringWriter wr = new StringWriter();
50  
51          int oneChar;
52  
53          while ((oneChar = rd.read()) != -1) {
54              wr.write(oneChar);
55          }
56  
57          rd.close();
58          wr.close();
59  
60          return wr.toString();
61      }
62  }