📄 file.java
字号:
int count = 0; for (int i = 0; i < fobjlist.length; i++) if (filter.accept(fobjlist[i]) == true) ++count; File[] final_list = new File[count]; count = 0; for (int i = 0; i < fobjlist.length; i++) if (filter.accept(fobjlist[i]) == true) { final_list[count] = fobjlist[i]; ++count; } return final_list; } /** * This method returns a <code>String</code> that is the path name of the * file as returned by <code>getPath</code>. * * @return A <code>String</code> representation of this file */ public String toString() { return path; } /** * @return A <code>URI</code> for this object. */ public URI toURI() { String abspath = getAbsolutePath(); if (isDirectory() || path.equals("")) abspath = abspath + separatorChar; if (separatorChar == '\\') abspath = separatorChar + abspath; try { return new URI("file", null, null, -1, abspath.replace(separatorChar, '/'), null, null); } catch (URISyntaxException use) { // Can't happen. throw (InternalError) new InternalError("Unconvertible file: " + this).initCause(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 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 VMFile.mkdir(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 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 synchronized 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 (! VMFile.exists(directory.path)) throw new IOException("System temporary directory " + directory.getName() + " does not exist."); if (! VMFile.isDirectory(directory.path)) 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. File file; if (!VMFile.IS_DOS_8_3) { do { long now = System.currentTimeMillis(); if (now > last_tmp) { // The last temporary file was created more than 1 ms ago. last_tmp = now; n_created = 0; } else n_created++; String name = Long.toHexString(now); if (n_created > 0) name += '_'+Integer.toHexString(n_created); String filename = prefix + name + suffix; file = new File(directory, filename); } while (VMFile.exists(file.path)); } else { // make sure prefix is not longer than 7 characters if (prefix.length() >= 8) throw new IllegalArgumentException("Prefix too long: " + prefix + "(valid length 3..7)"); long mask = 0x000000ffffFFFFL >> (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 (VMFile.exists(file.path)); } // 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 // XXX - FIXME race condition. VMFile.create(file.getAbsolutePath()); return file; } /** * 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(); // Test for existence. if (! VMFile.exists(path)) return false; return VMFile.setReadOnly(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() { return VMFile.listRoots(); } /** * 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 (VMFile.IS_CASE_SENSITIVE) 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 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(); dest.checkWrite(); // Call our native rename method return VMFile.renameTo(path, dest.path); } /** * 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 VMFile.setLastModified(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); } /** * 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); 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 + -