?? file.java
字號:
+ toString() + " to an URI").initCause(e); } } /* * This native method actually creates the directory */ private native boolean mkdirInternal (String path); /** * 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(); String mk_path = PlatformHelper.removeTailSeparator (path); return mkdirInternal (mk_path); } /** * 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() { String parent = getParent(); if (parent == null) { return mkdir(); } File f = new File(parent); if (!f.exists()) { boolean rc = f.mkdirs(); if (rc == false) return false; } return mkdir(); } /** * This method is used to create a temporary file */ private static native boolean createInternal (String name) throws IOException; /** * 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 = System.getProperty ("java.io.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"; /* Now identify a file name and make sure it doesn't exist. If the separator is '\' a DOS-style-filesystem is assumed and a 8+3-filename is used, otherwise use a long name. WARNGIN: some implementation of DOS-style-filesystems also accept '/' as separator. In that case the following code will fail. */ File file; if (separatorChar!='\\') { // probably a non-DOS-filesystem, use long names do { String filename = prefix + System.currentTimeMillis() + suffix; file = new File (directory, filename); } while (file.exists()); } else { // probably a DOS-filesystem, use short names (8+3) // make sure prefix is not longer than 7 characters if (prefix.length() >= 8) throw new IllegalArgumentException("Prefix too long: " + prefix + "(valid length 3..7)"); int mask = (int)(0x000000ffffFFFFL >> (long)(prefix.length() * 4)); do { int n = (int)(System.currentTimeMillis() & mask); String filename = prefix + java.lang.Integer.toHexString(n) + suffix; file = new File(directory, filename); } while (file.exists()); } // Verify that we are allowed to create this file SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkWrite(file.getAbsolutePath()); // Now create the file and return our file object createInternal(file.getAbsolutePath()); return file; } /* * This native method sets the permissions to make the file read only. */ private native boolean setReadOnlyInternal (String path); /** * 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() { // Test for existence. if (!exists()) return false; // We still need to do a SecurityCheck since exists() only checks // for read access checkWrite(); return setReadOnlyInternal (path); } /** * 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[] f = new File[1]; f[0] = new File("/"); return f; } /** * 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) { String p1, p2; try { p1 = getCanonicalPath(); p2 = other.getCanonicalPath(); } catch(IOException e) { // FIXME: What do we do here? The spec requires the canonical path. // Even if we don't call the method, we must replicate the functionality // which per the spec can fail. What happens in that situation? // I just assume the files are equal! return 0; } return p1.compareTo (p2); } /** * 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 renameToInternal(String target, String 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) { checkWrite(); // Call our native rename method return renameToInternal (path, dest.path); } /* * This method does the actual setting of the modification time. */ private native boolean setLastModifiedInternal (String path, 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 setLastModifiedInternal (path, 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); } static class DeleteFileHelper extends Thread { LinkedList filesToDelete = new LinkedList(); public DeleteFileHelper() { Runtime.getRuntime().addShutdownHook (this); } public void run() { Iterator fileIterator = filesToDelete.iterator(); while (fileIterator.hasNext()) { String path = (String)fileIterator.next(); try { File f = new File(path); f.delete(); } catch (Throwable _) { } } } } private static DeleteFileHelper deleteHelper = new DeleteFileHelper(); /** * 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 */ public void deleteOnExit() { // Check the SecurityManager SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkDelete(path); deleteHelper.filesToDelete.add(getAbsolutePath()); return; } 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 + -