* types of path separators ("/" versus "\", for example). It also
* contains method useful for creating and deleting files and directories.
*
- * @author Aaron M. Renn <arenn@urbanophile.com>
- * @author Tom Tromey <tromey@cygnus.com>
+ * @author Aaron M. Renn (arenn@urbanophile.com)
+ * @author Tom Tromey (tromey@cygnus.com)
*/
public class File implements Serializable, Comparable
{
* An example separator string would be "/" on the GNU system.
*/
public static final String separator = System.getProperty("file.separator");
+ private static final String dupSeparator = separator + separator;
/**
* This is the first character of the file separator string. On many
static final String tmpdir = System.getProperty("java.io.tmpdir");
static int maxPathLen;
static boolean caseSensitive;
- static String dupSeparator = separator + separator;
static
{
// On Windows, convert any '/' to '\'. This appears to be the same logic
// that Sun's Win32 Java performs.
if (separatorChar == '\\')
- p = p.replace ('/', '\\');
+ {
+ p = p.replace ('/', '\\');
+ // We have to special case the "\c:" prefix.
+ if (p.length() > 2 && p.charAt(0) == '\\' &&
+ ((p.charAt(1) >= 'a' && p.charAt(1) <= 'z') ||
+ (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z')) &&
+ p.charAt(2) == ':')
+ p = p.substring(1);
+ }
int dupIndex = p.indexOf(dupSeparator);
int plen = p.length();
this (directory == null ? null : directory.path, name);
}
+ /**
+ * This method initializes a new <code>File</code> object to represent
+ * a file corresponding to the specified <code>file:</code> protocol URI.
+ *
+ * @param uri The uri.
+ */
+ public File(URI uri)
+ {
+ if (uri == null)
+ throw new NullPointerException("uri is null");
+
+ if (!uri.getScheme().equals("file"))
+ throw new IllegalArgumentException("invalid uri protocol");
+
+ path = normalizePath(uri.getPath());
+ }
+
/**
* This method returns the path of this file as an absolute path name.
* If the path name is already absolute, then it is returned. Otherwise
* This method returns a <code>File</code> object representing the parent
* file of this one.
*
- * @param A <code>File</code> for the parent of this object.
+ * @return a <code>File</code> for the parent of this object.
* <code>null</code>
* will be returned if this object does not have a parent.
*
{
String dirname = tmpdir;
if (dirname == null)
- throw new IOException ("Cannot determine system temporary directory");
+ throw new IOException("Cannot determine system temporary directory");
- directory = new File (dirname);
+ directory = new File(dirname);
if (!directory.exists())
- throw new IOException ("System temporary directory "
- + directory.getName() + " does not exist.");
+ throw new IOException("System temporary directory "
+ + directory.getName() + " does not exist.");
if (!directory.isDirectory())
- throw new IOException ("System temporary directory "
- + directory.getName()
- + " is not really a directory.");
+ throw new IOException("System temporary directory "
+ + directory.getName()
+ + " is not really a directory.");
}
// Check if prefix is at least 3 characters long
*/
public boolean setReadOnly()
{
+ // Do a security check before trying to do anything else.
checkWrite();
return performSetReadOnly();
}
}
/**
- * Add this File to the set of files to be deleted upon normal
- * termination.
+ * Calling this method requests that the file represented by this object
+ * be deleted when the virtual machine exits. Note that this request cannot
+ * be cancelled. Also, it will only be carried out if the virtual machine
+ * exits normally.
*
* @exception SecurityException If deleting of the file is not allowed
*