Methods Summary |
---|
public static java.io.File | canonicalizeFile(java.io.File file)
if (file == null) throw new IllegalArgumentException ("null input: file");
try
{
return file.getCanonicalFile ();
}
catch (Exception e)
{
return file.getAbsoluteFile ();
}
|
public static java.lang.String | canonicalizePathname(java.lang.String pathname)Converts 'pathname' into the canonical OS form. This wrapper function
will return the absolute form of 'pathname' if File.getCanonicalPath() fails.
if (pathname == null) throw new IllegalArgumentException ("null input: pathname");
try
{
return new File (pathname).getCanonicalPath ();
}
catch (Exception e)
{
return new File (pathname).getAbsolutePath ();
}
|
public static java.io.File | createTempFile(java.io.File parentDir, java.lang.String prefix, java.lang.String extension)A slightly stricter version of File.createTempFile() in J2SDK 1.3: it requires
that the caller provide an existing parent directory for the temp file.
This defers to File.createTempFile (prefix, extension, parentDir) after
normalizing 'extension'.
MT-safety: if several threads use this API concurrently, the temp files
created are guaranteed to get created without any collisions and correspond
to files that did not exist before. However, if such a temp file is deleted
at a later point, this method may reuse its file name. These MT-safety
guarantees do not hold if files are created in the same directory outside
of this method.
if ((parentDir == null) || ! parentDir.exists ())
throw new IllegalArgumentException ("invalid parent directory: [" + parentDir + "]");
if ((prefix == null) || (prefix.length () < 3))
throw new IllegalArgumentException ("null or less than 3 chars long: " + prefix);
if (extension == null) extension = ".tmp";
else if (extension.charAt (0) != '.") extension = ".".concat (extension);
return File.createTempFile (prefix, extension, parentDir);
|
public static java.lang.String | getFileExtension(java.io.File file)Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).
if (file == null) throw new IllegalArgumentException ("null input: file");
final String name = file.getName ();
int lastDot = name.lastIndexOf ('.");
if (lastDot < 0) return "";
return name.substring (lastDot);
|
public static java.lang.String | getFileName(java.io.File file)Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).
if (file == null) throw new IllegalArgumentException ("null input: file");
final String name = file.getName ();
int lastDot = name.lastIndexOf ('.");
if (lastDot < 0) return name;
return name.substring (0, lastDot);
|
public static java.io.File | newFile(java.io.File dir, java.io.File file)
if (file == null) throw new IllegalArgumentException ("null input: file");
if ((dir == null) || file.isAbsolute ()) return file;
return new File (dir, file.getPath ());
|
public static java.io.File | newFile(java.io.File dir, java.lang.String file)
if (file == null) throw new IllegalArgumentException ("null input: file");
final File fileFile = new File (file);
if ((dir == null) || fileFile.isAbsolute ()) return fileFile;
return new File (dir, file);
|
public static java.io.File | newFile(java.lang.String dir, java.lang.String file)
if (file == null) throw new IllegalArgumentException ("null input: file");
final File fileFile = new File (file);
if ((dir == null) || fileFile.isAbsolute ()) return fileFile;
return new File (dir, file);
|
public static java.io.File[] | pathToFiles(java.lang.String[] path, boolean canonical)Converts an array of path segments to an array of Files. The order
of files follows the original order of path segments, except "duplicate"
entries are removed. The definition of duplicates depends on 'canonical':
- if 'canonical'=true, the pathnames are canonicalized via {@link #canonicalizePathname}
before they are compared for equality
- if 'canonical'=false, the pathnames are compared as case-sensitive strings
Note that duplicate removal in classpaths affects ClassLoader.getResources().
The first mode above makes the most sense, however the last one is what
Sun's java.net.URLClassLoader appears to do. Hence the last mode might be
necessary for reproducing its behavior in Sun-compatible JVMs.
if (path == null) throw new IllegalArgumentException ("null input: path");
if (path.length == 0) return IConstants.EMPTY_FILE_ARRAY;
final List /* Files */ _result = new ArrayList (path.length);
final Set /* String */ pathnames = new HashSet (path.length);
final String separators = ",".concat (File.pathSeparator);
for (int i = 0; i < path.length; ++ i)
{
String segment = path [i];
if (segment == null) throw new IllegalArgumentException ("null input: path[" + i + "]");
final StringTokenizer tokenizer = new StringTokenizer (segment, separators);
while (tokenizer.hasMoreTokens ())
{
String pathname = tokenizer.nextToken ();
if (canonical) pathname = canonicalizePathname (pathname);
if (pathnames.add (pathname))
{
_result.add (new File (pathname));
}
}
}
final File [] result = new File [_result.size ()];
_result.toArray (result);
return result;
|
public static java.lang.String[] | readFileList(java.io.File atfile)No duplicate elimination.
if (atfile == null) throw new IllegalArgumentException ("null input: atfile");
List _result = null;
BufferedReader in = null;
try
{
in = new BufferedReader (new FileReader (atfile), 8 * 1024); // uses default encoding
_result = new LinkedList ();
for (String line; (line = in.readLine ()) != null; )
{
line = line.trim ();
if ((line.length () == 0) || (line.charAt (0) == '#")) continue;
_result.add (line);
}
}
finally
{
if (in != null) try { in.close (); } catch (Exception ignore) {}
}
if ((_result == null) || _result.isEmpty ())
return IConstants.EMPTY_STRING_ARRAY;
else
{
final String [] result = new String [_result.size ()];
_result.toArray (result);
return result;
}
|
public static boolean | renameFile(java.io.File source, java.io.File target, boolean overwrite)Renames 'source' to 'target' [intermediate directories are created if necessary]. If
'target' exists and 'overwrite' is false, the method is a no-op. No exceptions are
thrown except for when input is invalid. If the operation fails half-way it can leave
some file system artifacts behind.
if ((source == null) || ! source.exists ())
throw new IllegalArgumentException ("invalid input source: [" + source + "]");
if (target == null)
throw new IllegalArgumentException ("null input: target");
final boolean targetExists;
if (! (targetExists = target.exists ()) || overwrite)
{
if (targetExists)
{
// need to delete the target first or the rename will fail:
target.delete (); // not checking the result here: let the rename fail later
}
else
{
// note that File.renameTo() does not create intermediate directories on demand:
final File targetDir = target.getParentFile ();
if ((targetDir != null) && ! targetDir.equals (source.getParentFile ()))
targetDir.mkdirs (); // TODO: clean this up on failure?
}
// note: this can fail for a number of reasons, including the target
// being on a different drive/file system:
return source.renameTo (target);
}
return false;
|