1 /*** 2 * The contents of this file are subject to the Mozilla Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/MPL/ 6 * 7 * Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 * implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is pow2toolkit library. 13 * 14 * The Initial Owner of the Original Code is 15 * Power Of Two S.R.L. (www.pow2.com) 16 * 17 * Portions created by Power Of Two S.R.L. are 18 * Copyright (C) Power Of Two S.R.L. 19 * All Rights Reserved. 20 * 21 * Contributor(s): 22 */ 23 24 package com.pow2.util; 25 26 import java.io.*; 27 import java.lang.StringBuffer; 28 import java.util.Properties; 29 30 import org.apache.log4j.Category; 31 32 /*** 33 * FileUtil class. 34 * <br> 35 * Contains static methods for file access. 36 * 37 * @author foxat 38 * @created 24 giugno 2002 39 */ 40 public class FileUtil 41 { 42 /*** log4j category */ 43 private final static Category cat = Category.getInstance(FileUtil.class); 44 45 46 /*** 47 * Read the text file whose fullpath is specified by the input <code>file</code> parameter, 48 * and return the StringBuffer object containing its content. 49 * 50 * @param file the file fullpath 51 * @return the StringBuffer object containing the file content 52 * @exception Exception if any error occurs. 53 */ 54 public static StringBuffer read(String file) throws Exception 55 { 56 BufferedReader in = null; 57 StringBuffer sb = new StringBuffer(); 58 String s = null; 59 60 try 61 { 62 in = new BufferedReader(new FileReader(file)); 63 64 while ((s = in.readLine()) != null) 65 sb.append(s).append('\n'); 66 67 return sb; 68 } 69 finally 70 { 71 if (in != null) 72 in.close(); 73 } 74 } 75 76 77 /*** 78 * Read the Java properties file specified by the input <code>propsFile</code> 79 * parameter, and use its content to instance a new Properties object. 80 * 81 * @param propsFile the name of the Java Properties file to read 82 * @return the Properties object containing the properties readed 83 * from the input file 84 * @exception Exception if any error occcurs. 85 */ 86 public static Properties getProperties(String propsFile) throws Exception 87 { 88 return getProperties(propsFile, false); 89 } 90 91 92 /*** 93 * Read the Java properties file specified by the input <code>propsFile</code> 94 * parameter, and use its content to instance a new Properties object. 95 * 96 * @param propsFile the name of the Java Properties file to read 97 * @param addToSystemProps Description of the Parameter 98 * @return the Properties object containing the properties readed 99 * from the input file 100 * @exception Exception if any error occcurs. 101 */ 102 public static Properties getProperties(String propsFile, boolean addToSystemProps) throws Exception 103 { 104 FileInputStream fis = null; 105 Properties props = null; 106 107 try 108 { 109 fis = new FileInputStream(propsFile); 110 props = (addToSystemProps) ? new Properties(System.getProperties()) : new Properties(); 111 props.load(fis); 112 fis.close(); 113 } 114 finally 115 { 116 if (fis != null) 117 fis.close(); 118 } 119 120 return props; 121 } 122 }