📄 file.java
字号:
String abspath = getAbsolutePath(); if (isDirectory()) abspath = abspath + separator; try { return new URI("file", abspath.replace(separatorChar, '/'), null); } catch (URISyntaxException use) { // Can't happen. throw new RuntimeException(use); } } /** * This method returns a <code>URL</code> with the <code>file:</code> * protocol that represents this file. The exact form of this URL is * system dependent. * * @return A <code>URL</code> for this object. * * @exception MalformedURLException If the URL cannot be created * successfully. */ public URL toURL() throws MalformedURLException { // On Win32, Sun's JDK returns URLs of the form "file:/c:/foo/bar.txt", // while on UNIX, it returns URLs of the form "file:/foo/bar.txt". if (separatorChar == '\\') return new URL ("file:/" + getAbsolutePath().replace ('\\', '/') + (isDirectory() ? "/" : "")); else return new URL ("file:" + getAbsolutePath() + (isDirectory() ? "/" : "")); } /* * This native method actually creates the directory */ private final native boolean performMkdir(); /** * This method creates a directory for the path represented by this object. * * @return <code>true</code> if the directory was created, * <code>false</code> otherwise * * @exception SecurityException If write access is not allowed to this file */ public boolean mkdir() { checkWrite(); return performMkdir(); } private static boolean mkdirs (File x) { if (x.isDirectory()) return true; String p = x.getPath(); String parent = x.getParent(); if (parent != null) { x.path = parent; if (! mkdirs (x)) return false; x.path = p; } return x.mkdir(); } /** * This method creates a directory for the path represented by this file. * It will also create any intervening parent directories if necessary. * * @return <code>true</code> if the directory was created, * <code>false</code> otherwise * * @exception SecurityException If write access is not allowed to this file */ public boolean mkdirs() { checkWrite(); if (isDirectory()) return false; return mkdirs (new File (path)); } private static synchronized String nextValue() { return Long.toString(counter++, Character.MAX_RADIX); } /** * This method creates a temporary file in the specified directory. If * the directory name is null, then this method uses the system temporary * directory. The files created are guaranteed not to currently exist and * the same file name will never be used twice in the same virtual * machine instance. * The system temporary directory is determined by examinging the * <code>java.io.tmpdir</code> system property. * <p> * The <code>prefix</code> parameter is a sequence of at least three * characters that are used as the start of the generated filename. The * <code>suffix</code> parameter is a sequence of characters that is used * to terminate the file name. This parameter may be <code>null</code> * and if it is, the suffix defaults to ".tmp". * <p> * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code> * method is used to verify that this operation is permitted. * * @param prefix The character prefix to use in generating the path name. * @param suffix The character suffix to use in generating the path name. * @param directory The directory to create the file in, or * <code>null</code> for the default temporary directory * * @exception IllegalArgumentException If the patterns is not valid * @exception SecurityException If there is no permission to perform * this operation * @exception IOException If an error occurs * * @since 1.2 */ public static File createTempFile(String prefix, String suffix, File directory) throws IOException { // Grab the system temp directory if necessary if (directory == null) { String dirname = tmpdir; if (dirname == null) throw new IOException("Cannot determine system temporary directory"); directory = new File(dirname); if (!directory.exists()) 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."); } // Check if prefix is at least 3 characters long if (prefix.length() < 3) throw new IllegalArgumentException("Prefix too short: " + prefix); // Set default value of suffix if (suffix == null) suffix = ".tmp"; // Truncation rules. // `6' is the number of characters we generate. if (prefix.length() + 6 + suffix.length() > maxPathLen) { int suf_len = 0; if (suffix.charAt(0) == '.') suf_len = 4; suffix = suffix.substring(0, suf_len); if (prefix.length() + 6 + suf_len > maxPathLen) prefix = prefix.substring(0, maxPathLen - 6 - suf_len); } File f; // How many times should we try? We choose 100. for (int i = 0; i < 100; ++i) { // This is ugly. String t = "ZZZZZZ" + nextValue(); String l = prefix + t.substring(t.length() - 6) + suffix; try { f = new File(directory, l); if (f.createNewFile()) return f; } catch (IOException ignored) { } } throw new IOException ("cannot create temporary file"); } /* * This native method sets the permissions to make the file read only. */ private native boolean performSetReadOnly(); /** * This method sets the file represented by this object to be read only. * A read only file or directory cannot be modified. Please note that * GNU systems allow read only files to be deleted if the directory it * is contained in is writable. * * @return <code>true</code> if the operation succeeded, <code>false</code> * otherwise. * * @exception SecurityException If the <code>SecurityManager</code> does * not allow this operation. * * @since 1.2 */ public boolean setReadOnly() { // Do a security check before trying to do anything else. checkWrite(); return performSetReadOnly(); } private static native File[] performListRoots(); /** * This method returns an array of filesystem roots. Some operating systems * have volume oriented filesystem. This method provides a mechanism for * determining which volumes exist. GNU systems use a single hierarchical * filesystem, so will have only one "/" filesystem root. * * @return An array of <code>File</code> objects for each filesystem root * available. * * @since 1.2 */ public static File[] listRoots() { File[] roots = performListRoots(); SecurityManager s = System.getSecurityManager(); if (s != null) { // Only return roots to which the security manager permits read access. int count = roots.length; for (int i = 0; i < roots.length; i++) { try { s.checkRead (roots[i].path); } catch (SecurityException sx) { roots[i] = null; count--; } } if (count != roots.length) { File[] newRoots = new File[count]; int k = 0; for (int i=0; i < roots.length; i++) { if (roots[i] != null) newRoots[k++] = roots[i]; } roots = newRoots; } } return roots; } /** * This method creates a temporary file in the system temporary directory. * The files created are guaranteed not to currently exist and the same file * name will never be used twice in the same virtual machine instance. The * system temporary directory is determined by examinging the * <code>java.io.tmpdir</code> system property. * <p> * The <code>prefix</code> parameter is a sequence of at least three * characters that are used as the start of the generated filename. The * <code>suffix</code> parameter is a sequence of characters that is used * to terminate the file name. This parameter may be <code>null</code> * and if it is, the suffix defaults to ".tmp". * <p> * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code> * method is used to verify that this operation is permitted. * <p> * This method is identical to calling * <code>createTempFile(prefix, suffix, null)</code>. * * @param prefix The character prefix to use in generating the path name. * @param suffix The character suffix to use in generating the path name. * * @exception IllegalArgumentException If the prefix or suffix are not valid. * @exception SecurityException If there is no permission to perform * this operation * @exception IOException If an error occurs */ public static File createTempFile(String prefix, String suffix) throws IOException { return createTempFile(prefix, suffix, null); } /** * This method compares the specified <code>File</code> to this one * to test for equality. It does this by comparing the canonical path names * of the files. * <p> * The canonical paths of the files are determined by calling the * <code>getCanonicalPath</code> method on each object. * <p> * This method returns a 0 if the specified <code>Object</code> is equal * to this one, a negative value if it is less than this one * a positive value if it is greater than this one. * * @return An integer as described above * * @since 1.2 */ public int compareTo(File other) { if (caseSensitive) return path.compareTo (other.path); else return path.compareToIgnoreCase (other.path); } /** * This method compares the specified <code>Object</code> to this one * to test for equality. It does this by comparing the canonical path names * of the files. This method is identical to <code>compareTo(File)</code> * except that if the <code>Object</code> passed to it is not a * <code>File</code>, it throws a <code>ClassCastException</code> * <p> * The canonical paths of the files are determined by calling the * <code>getCanonicalPath</code> method on each object. * <p> * This method returns a 0 if the specified <code>Object</code> is equal * to this one, a negative value if it is less than this one * a positive value if it is greater than this one. * * @return An integer as described above * * @exception ClassCastException If the passed <code>Object</code> is * not a <code>File</code> * * @since 1.2 */ public int compareTo(Object obj) { return compareTo((File) obj); } /* * This native method actually performs the rename. */ private native boolean performRenameTo (File dest); /** * This method renames the file represented by this object to the path * of the file represented by the argument <code>File</code>. * * @param dest The <code>File</code> object representing the target name * * @return <code>true</code> if the rename succeeds, <code>false</code> * otherwise. * * @exception SecurityException If write access is not allowed to the * file by the <code>SecurityMananger</code>. */ public synchronized boolean renameTo(File dest) { SecurityManager s = System.getSecurityManager(); String sname = getName(); String dname = dest.getName(); if (s != null) { s.checkWrite (sname); s.checkWrite (dname); } return performRenameTo (dest); } /* * This method does the actual setting of the modification time. */ private native boolean performSetLastModified(long time); /** * This method sets the modification time on the file to the specified * value. This is specified as the number of seconds since midnight * on January 1, 1970 GMT. * * @param time The desired modification time. * * @return <code>true</code> if the operation succeeded, <code>false</code> * otherwise. * * @exception IllegalArgumentException If the specified time is negative. * @exception SecurityException If the <code>SecurityManager</code> will * not allow this operation. * * @since 1.2 */ public boolean setLastModified(long time) { if (time < 0) throw new IllegalArgumentException("Negative modification time: " + time); checkWrite(); return performSetLastModified(time); } private void checkWrite() { // Check the SecurityManager SecurityManager s = System.getSecurityManager(); if (s != null) s.checkWrite(path); } private void checkRead() { // Check the SecurityManager SecurityManager s = System.getSecurityManager(); if (s != null) s.checkRead(path); } /** * 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 * * @since 1.2 */ // FIXME: This should use the ShutdownHook API once we implement that. public void deleteOnExit() { // Check the SecurityManager SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkDelete (getName()); DeleteFileHelper.add(this); } private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); oos.writeChar(separatorChar); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); // If the file was from an OS with a different dir separator, // fixup the path to use the separator on this OS. char oldSeparatorChar = ois.readChar(); if (oldSeparatorChar != separatorChar) path = path.replace(oldSeparatorChar, separatorChar); } } // class File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -