1 9 package com.vladium.util; 10 11 import java.io.BufferedReader ; 12 import java.io.File ; 13 import java.io.FileReader ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.LinkedList ; 18 import java.util.List ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 26 public 27 abstract class Files 28 { 29 31 36 public static String [] readFileList (final File atfile) 37 throws IOException 38 { 39 if (atfile == null) throw new IllegalArgumentException ("null input: atfile"); 40 41 List _result = null; 42 43 BufferedReader in = null; 44 try 45 { 46 in = new BufferedReader (new FileReader (atfile), 8 * 1024); _result = new LinkedList (); 48 49 for (String line; (line = in.readLine ()) != null; ) 50 { 51 line = line.trim (); 52 if ((line.length () == 0) || (line.charAt (0) == '#')) continue; 53 54 _result.add (line); 55 } 56 } 57 finally 58 { 59 if (in != null) try { in.close (); } catch (Exception ignore) {} 60 } 61 62 if ((_result == null) || _result.isEmpty ()) 63 return IConstants.EMPTY_STRING_ARRAY; 64 else 65 { 66 final String [] result = new String [_result.size ()]; 67 _result.toArray (result); 68 69 return result; 70 } 71 } 72 73 88 public static File [] pathToFiles (final String [] path, final boolean canonical) 89 { 90 if (path == null) throw new IllegalArgumentException ("null input: path"); 91 if (path.length == 0) return IConstants.EMPTY_FILE_ARRAY; 92 93 final List _result = new ArrayList (path.length); 94 final Set pathnames = new HashSet (path.length); 95 96 final String separators = ",".concat (File.pathSeparator); 97 98 for (int i = 0; i < path.length; ++ i) 99 { 100 String segment = path [i]; 101 if (segment == null) throw new IllegalArgumentException ("null input: path[" + i + "]"); 102 103 final StringTokenizer tokenizer = new StringTokenizer (segment, separators); 104 while (tokenizer.hasMoreTokens ()) 105 { 106 String pathname = tokenizer.nextToken (); 107 108 if (canonical) pathname = canonicalizePathname (pathname); 109 110 if (pathnames.add (pathname)) 111 { 112 _result.add (new File (pathname)); 113 } 114 } 115 } 116 117 final File [] result = new File [_result.size ()]; 118 _result.toArray (result); 119 120 return result; 121 } 122 123 127 public static String canonicalizePathname (final String pathname) 128 { 129 if (pathname == null) throw new IllegalArgumentException ("null input: pathname"); 130 131 try 132 { 133 return new File (pathname).getCanonicalPath (); 134 } 135 catch (Exception e) 136 { 137 return new File (pathname).getAbsolutePath (); 138 } 139 } 140 141 public static File canonicalizeFile (final File file) 142 { 143 if (file == null) throw new IllegalArgumentException ("null input: file"); 144 145 try 146 { 147 return file.getCanonicalFile (); 148 } 149 catch (Exception e) 150 { 151 return file.getAbsoluteFile (); 152 } 153 } 154 155 165 public static String getFileName (final File file) 166 { 167 if (file == null) throw new IllegalArgumentException ("null input: file"); 168 169 final String name = file.getName (); 170 int lastDot = name.lastIndexOf ('.'); 171 if (lastDot < 0) return name; 172 173 return name.substring (0, lastDot); 174 } 175 176 186 public static String getFileExtension (final File file) 187 { 188 if (file == null) throw new IllegalArgumentException ("null input: file"); 189 190 final String name = file.getName (); 191 int lastDot = name.lastIndexOf ('.'); 192 if (lastDot < 0) return ""; 193 194 return name.substring (lastDot); 195 } 196 197 203 public static File newFile (final File dir, final File file) 204 { 205 if (file == null) throw new IllegalArgumentException ("null input: file"); 206 207 if ((dir == null) || file.isAbsolute ()) return file; 208 209 return new File (dir, file.getPath ()); 210 } 211 212 218 public static File newFile (final File dir, final String file) 219 { 220 if (file == null) throw new IllegalArgumentException ("null input: file"); 221 222 final File fileFile = new File (file); 223 if ((dir == null) || fileFile.isAbsolute ()) return fileFile; 224 225 return new File (dir, file); 226 } 227 228 234 public static File newFile (final String dir, final String file) 235 { 236 if (file == null) throw new IllegalArgumentException ("null input: file"); 237 238 final File fileFile = new File (file); 239 if ((dir == null) || fileFile.isAbsolute ()) return fileFile; 240 241 return new File (dir, file); 242 } 243 244 260 public static boolean renameFile (final File source, final File target, final boolean overwrite) 261 { 262 if ((source == null) || ! source.exists ()) 263 throw new IllegalArgumentException ("invalid input source: [" + source + "]"); 264 if (target == null) 265 throw new IllegalArgumentException ("null input: target"); 266 267 final boolean targetExists; 268 if (! (targetExists = target.exists ()) || overwrite) 269 { 270 if (targetExists) 271 { 272 target.delete (); } 275 else 276 { 277 final File targetDir = target.getParentFile (); 279 if ((targetDir != null) && ! targetDir.equals (source.getParentFile ())) 280 targetDir.mkdirs (); } 282 283 return source.renameTo (target); 286 } 287 288 return false; 289 } 290 291 314 public static File createTempFile (final File parentDir, final String prefix, String extension) 315 throws IOException 316 { 317 if ((parentDir == null) || ! parentDir.exists ()) 318 throw new IllegalArgumentException ("invalid parent directory: [" + parentDir + "]"); 319 if ((prefix == null) || (prefix.length () < 3)) 320 throw new IllegalArgumentException ("null or less than 3 chars long: " + prefix); 321 322 if (extension == null) extension = ".tmp"; 323 else if (extension.charAt (0) != '.') extension = ".".concat (extension); 324 325 return File.createTempFile (prefix, extension, parentDir); 326 } 327 328 330 332 334 335 private Files () {} 337 } | Popular Tags |