View Javadoc

1   /*
2    * $Id: FileUtils.java 1256 2009-01-09 08:26:35Z andreas.hoheisel@first.fraunhofer.de $
3    *
4    * Copyright 2008 Fraunhofer Gesellschaft, Munich, Germany,
5    * for its Fraunhofer Institute for Computer Architecture and Software Technology (FIRST), Berlin, Germany
6    * All rights reserved.
7    *
8    * See http://www.first.fraunhofer.de and http://www.gridworkflow.org/gwes for more details.
9    */
10  
11  package net.kwfgrid.gworkflowdl.conversion;
12  
13  import java.io.*;
14  
15  /***
16   * @author Andreas Hoheisel
17   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
18   * @version $Id: FileUtils.java 1256 2009-01-09 08:26:35Z andreas.hoheisel@first.fraunhofer.de $
19   */
20  public class FileUtils {
21  
22      private FileUtils() {
23          // only static methods.
24      }
25  
26      /***
27       * Read a file and put the contents to a string
28       *
29       * @param fileName File name of the file to read
30       * @return String with the contents of the file
31       */
32      public static String readFile(String fileName) throws IOException {
33          StringBuffer buffer = new StringBuffer();
34          BufferedReader fileReader;
35          fileReader = new BufferedReader(new FileReader(fileName));
36  
37          String line;
38          line = fileReader.readLine();
39          while (line != null) {
40              buffer.append(line);
41              buffer.append('\n');
42              line = fileReader.readLine();
43          }
44          return buffer.toString();
45      }
46  
47      public static void writeFile(String fileName, String contents) throws IOException {
48          FileWriter fstream = new FileWriter(fileName);
49          BufferedWriter out = new BufferedWriter(fstream);
50          out.write(contents);
51          out.close();
52      }
53      
54  }