KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > util > WSUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.websphere6.util;
20
21 import java.io.*;
22
23 import org.openide.*;
24
25 /**
26  * Just a collection of static utility methods
27  *
28  * @author Kirill Sorokin
29  */

30 public class WSUtil {
31
32     private static Class JavaDoc clazz = new WSUtil().getClass();
33
34     /**
35      * Reads an entire text file into a string
36      *
37      * @param file the file's handle
38      * @return a string with the file's contents
39      */

40     public static String JavaDoc readFile(File file) {
41        // init the buffer for storing the file's contents
42
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
43        
44        try {
45            // init the reader
46
LineNumberReader reader = new LineNumberReader(new FileReader(file));
47            
48            // init the temp line
49
String JavaDoc temp = "";
50            
51            // read the file
52
while ((temp = reader.readLine()) != null) {
53                buffer.append(temp).append("\n");
54            }
55            
56            if (WSDebug.isEnabled())
57                WSDebug.notify(clazz, "read string: \n" + buffer.toString());
58            
59            // return the string
60
return buffer.toString();
61        } catch (IOException e) {
62            ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
63        }
64        
65        return null;
66     }
67     
68     /**
69      * Writes the supplied string to a file overwriting the previous contents
70      *
71      * @param file the file to write to
72      * @param the new file contents
73      */

74     public static void writeFile(File file, String JavaDoc contents) {
75         try {
76             if (WSDebug.isEnabled())
77                 WSDebug.notify(clazz, "write string: \n" + contents);
78            
79             // create a writer and write the contents
80
new FileOutputStream(file).write(contents.getBytes());
81         } catch (IOException e) {
82             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
83         }
84     }
85 }
Popular Tags