1 37 38 package net.sourceforge.cobertura.javancss; 39 40 import java.io.BufferedReader ; 41 import java.io.File ; 42 import java.io.FileNotFoundException ; 43 import java.io.FileReader ; 44 import java.io.IOException ; 45 46 63 public class FileUtil 64 { 65 66 69 private FileUtil() 70 { 71 super(); 72 } 73 74 91 private static String concatPath(String sPath_, String sFile_) 92 { 93 Util.panicIf(sPath_ == null); 94 97 String sRetVal = sPath_; 98 99 if (!Util.isEmpty(sFile_)) 100 { 101 if (sPath_.length() > 0 && !sPath_.endsWith(File.separator)) 102 { 103 sRetVal += File.separator; 104 } 105 106 sRetVal += sFile_; 107 } 108 109 return sRetVal; 110 } 111 112 126 public static String readFile(String sFileName_) throws IOException , FileNotFoundException 127 { 128 StringBuffer sFileContent = new StringBuffer (100000); 129 130 try 131 { 132 FileReader frIni = new FileReader (sFileName_); 133 if (frIni != null) 134 { 135 BufferedReader brIni = new BufferedReader (frIni); 136 if (brIni != null) 137 { 138 while (brIni.ready()) 139 { 140 String sLine = brIni.readLine(); 141 if (sLine == null) 142 { 143 break; 144 } 145 sFileContent.append(sLine).append('\n'); 146 } 147 brIni.close(); 148 } 149 frIni.close(); 150 } 151 } 152 catch (FileNotFoundException fileNotFoundException) 153 { 154 throw new FileNotFoundException ("No such file: '" + sFileName_ + "'"); 155 } 156 157 return sFileContent.toString(); 158 } 159 160 163 private static String getAbsoluteFileName(String sFileName_) 164 { 165 String sRetVal = null; 166 167 try 168 { 169 File pFile = new File (sFileName_); 170 sRetVal = pFile.getCanonicalPath(); 171 } 172 catch (Exception e) 173 { 174 return null; 175 } 176 177 return sRetVal; 178 } 179 180 189 public static String normalizeFileName(String sFile) 190 { 191 return normalizeFileName(sFile, (String )System.getProperties().get("user.dir")); 192 } 193 194 203 private static String normalizeFileName(String sFile, String sUserDir) 204 { 205 sFile = sFile.trim(); 206 if (Util.isEmpty(sFile) || sFile.equals(".")) 207 { 208 sFile = sUserDir; 209 } 210 else if (!FileUtil.isAbsolute(sFile)) 211 { 212 sFile = FileUtil.concatPath(sUserDir, sFile); 213 } 214 sFile = FileUtil.getAbsoluteFileName(sFile); 215 216 return sFile; 217 } 218 219 227 private static boolean isAbsolute(String sFileName_) 228 { 229 return new File (sFileName_).isAbsolute(); 230 } 231 232 } 233 | Popular Tags |