📄 file.java
字号:
{ // On Windows, a process has a current working directory for // each drive and a path like "G:foo\bar" would mean the // absolute path "G:\wombat\foo\bar" if "\wombat" is the // working directory on the G drive. String drvDir = null; try { drvDir = new File (path.substring (0, 2)).getCanonicalPath(); } catch (IOException e) { drvDir = path.substring (0, 2) + "\\"; } // Note: this would return "C:\\." for the path "C:.", if "\" // is the working folder on the C drive, but this is // consistent with what Sun's JRE 1.4.1.01 actually returns! if (path.length() > 2) return drvDir + '\\' + path.substring (2, path.length()); else return drvDir; } else return System.getProperty ("user.dir") + separatorChar + path; } /** * This method returns a <code>File</code> object representing the * absolute path of this object. * * @return A <code>File</code> with the absolute path of the object. * * @since 1.2 */ public File getAbsoluteFile() { return new File(getAbsolutePath()); } /** * This method returns a canonical representation of the pathname of * this file. The actual form of the canonical representation is * different. On the GNU system, the canonical form differs from the * absolute form in that all relative file references to "." and ".." * are resolved and removed. * <p> * Note that this method, unlike the other methods which return path * names, can throw an IOException. This is because native method * might be required in order to resolve the canonical path * * @exception IOException If an error occurs */ public native String getCanonicalPath() throws IOException; /** * This method returns a <code>File</code> object representing the * canonical path of this object. * * @return A <code>File</code> instance representing the canonical path of * this object. * * @exception IOException If an error occurs. * * @since 1.2 */ public File getCanonicalFile() throws IOException { return new File(getCanonicalPath()); } /** * This method returns the name of the file. This is everything in the * complete path of the file after the last instance of the separator * string. * * @return The file name */ public String getName() { int nameSeqIndex = 0; if (separatorChar == '\\' && path.length() > 1) { // On Windows, ignore the drive specifier or the leading '\\' // of a UNC network path, if any (a.k.a. the "prefix"). if ((path.charAt (0) == '\\' && path.charAt (1) == '\\') || (((path.charAt (0) >= 'a' && path.charAt (0) <= 'z') || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')) && path.charAt (1) == ':')) { if (path.length() > 2) nameSeqIndex = 2; else return ""; } } String nameSeq = (nameSeqIndex > 0 ? path.substring (nameSeqIndex) : path); int last = nameSeq.lastIndexOf (separatorChar); return nameSeq.substring (last + 1); } /** * This method returns a <code>String</code> the represents this file's * parent. <code>null</code> is returned if the file has no parent. The * parent is determined via a simple operation which removes the * * @return The parent directory of this file */ public String getParent() { String prefix = null; int nameSeqIndex = 0; // The "prefix", if present, is the leading "/" on UNIX and // either the drive specifier (e.g. "C:") or the leading "\\" // of a UNC network path on Windows. if (separatorChar == '/' && path.charAt (0) == '/') { prefix = "/"; nameSeqIndex = 1; } else if (separatorChar == '\\' && path.length() > 1) { if ((path.charAt (0) == '\\' && path.charAt (1) == '\\') || (((path.charAt (0) >= 'a' && path.charAt (0) <= 'z') || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')) && path.charAt (1) == ':')) { prefix = path.substring (0, 2); nameSeqIndex = 2; } } // According to the JDK docs, the returned parent path is the // portion of the name sequence before the last separator // character, if found, prefixed by the prefix, otherwise null. if (nameSeqIndex < path.length()) { String nameSeq = path.substring (nameSeqIndex, path.length()); int last = nameSeq.lastIndexOf (separatorChar); if (last == -1) return prefix; else if (last == (nameSeq.length() - 1)) // Note: The path would not have a trailing separator // except for cases like "C:\" on Windows (see // normalizePath( )), where Sun's JRE 1.4 returns null. return null; else if (last == 0) last++; if (prefix != null) return prefix + nameSeq.substring (0, last); else return nameSeq.substring (0, last); } else // Sun's JRE 1.4 returns null if the prefix is the only // component of the path - so "/" gives null on UNIX and // "C:", "\\", etc. return null on Windows. return null; } /** * This method returns a <code>File</code> object representing the parent * file of this one. * * @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. * * @since 1.2 */ public File getParentFile() { String parent = getParent(); return parent != null ? new File(parent) : null; } /** * Returns the path name that represents this file. May be a relative * or an absolute path name * * @return The pathname of this file */ public String getPath() { return path; } /** * This method returns a hash code representing this file. It is the * hash code of the path of this file (as returned by <code>getPath()</code>) * exclusived or-ed with the value 1234321. * * @return The hash code for this object */ public int hashCode() { if (caseSensitive) return path.hashCode() ^ 1234321; else return path.toLowerCase().hashCode() ^ 1234321; } /** * This method returns true if this object represents an absolute file * path and false if it does not. The definition of an absolute path varies * by system. As an example, on GNU systems, a path is absolute if it starts * with a "/". * * @return <code>true</code> if this object represents an absolute * file name, <code>false</code> otherwise. */ public native boolean isAbsolute(); /** * This method tests whether or not the file represented by this object * is a directory. In order for this method to return <code>true</code>, * the file represented by this object must exist and be a directory. * * @return <code>true</code> if this file is a directory, <code>false</code> * otherwise * * @exception SecurityException If reading of the file is not permitted */ public boolean isDirectory() { checkRead(); return _stat (DIRECTORY); } /** * This method tests whether or not the file represented by this object * is a "plain" file. A file is a plain file if and only if it 1) Exists, * 2) Is not a directory or other type of special file. * * @return <code>true</code> if this is a plain file, <code>false</code> * otherwise * * @exception SecurityException If reading of the file is not permitted */ public boolean isFile() { checkRead(); return _stat (ISFILE); } /** * This method tests whether or not this file represents a "hidden" file. * On GNU systems, a file is hidden if its name begins with a "." * character. Files with these names are traditionally not shown with * directory listing tools. * * @return <code>true</code> if the file is hidden, <code>false</code> * otherwise. * * @since 1.2 */ public boolean isHidden() { checkRead(); return _stat (ISHIDDEN); } /** * This method returns the last modification time of this file. The * time value returned is an abstract value that should not be interpreted * as a specified time value. It is only useful for comparing to other * such time values returned on the same system. In that case, the larger * value indicates a more recent modification time. * <p> * If the file does not exist, then a value of 0 is returned. * * @return The last modification time of the file * * @exception SecurityException If reading of the file is not permitted */ public long lastModified() { checkRead(); return attr (MODIFIED); } /** * This method returns the length of the file represented by this object, * or 0 if the specified file does not exist. * * @return The length of the file * * @exception SecurityException If reading of the file is not permitted */ public long length() { checkRead(); return attr (LENGTH); } /* * This native function actually produces the list of file in this * directory */ private final native Object[] performList (FilenameFilter filter, FileFilter fileFilter, Class result_type); /** * This method returns a array of <code>String</code>'s representing the * list of files is then directory represented by this object. If this * object represents a non-directory file or a non-existent file, then * <code>null</code> is returned. The list of files will not contain * any names such as "." or ".." which indicate the current or parent * directory. Also, the names are not guaranteed to be sorted. * <p> * In this form of the <code>list()</code> method, a filter is specified * that allows the caller to control which files are returned in the * list. The <code>FilenameFilter</code> specified is called for each * file returned to determine whether or not that file should be included * in the list. * <p> * A <code>SecurityManager</code> check is made prior to reading the * directory. If read access to the directory is denied, an exception * will be thrown. * * @param filter An object which will identify files to exclude from * the directory listing. * * @return An array of files in the directory, or <code>null</code> * if this object does not represent a valid directory. * * @exception SecurityException If read access is not allowed to the * directory by the <code>SecurityManager</code> */ public String[] list(FilenameFilter filter) { checkRead(); return (String[]) performList (filter, null, String.class); } /** * This method returns a array of <code>String</code>'s representing the * list of files is then directory represented by this object. If this * object represents a non-directory file or a non-existent file, then * <code>null</code> is returned. The list of files will not contain * any names such as "." or ".." which indicate the current or parent * directory. Also, the names are not guaranteed to be sorted. * <p> * A <code>SecurityManager</code> check is made prior to reading the * directory. If read access to the directory is denied, an exception * will be thrown. * * @return An array of files in the directory, or <code>null</code> if * this object does not represent a valid directory. * * @exception SecurityException If read access is not allowed to the * directory by the <code>SecurityManager</code> */ public String[] list() { checkRead(); return (String[]) performList (null, null, String.class); } /** * This method returns an array of <code>File</code> objects representing * all the files in the directory represented by this object. If this * object does not represent a directory, <code>null</code> is returned. * Each of the returned <code>File</code> object is constructed with this * object as its parent. * <p> * A <code>SecurityManager</code> check is made prior to reading the * directory. If read access to the directory is denied, an exception * will be thrown. * * @return An array of <code>File</code> objects for this directory. * * @exception SecurityException If the <code>SecurityManager</code> denies * access to this directory. * * @since 1.2 */ public File[] listFiles() { checkRead(); return (File[]) performList (null, null, File.class); } /** * This method returns an array of <code>File</code> objects representing * all the files in the directory represented by this object. If this * object does not represent a directory, <code>null</code> is returned. * Each of the returned <code>File</code> object is constructed with this * object as its parent. * <p> * In this form of the <code>listFiles()</code> method, a filter is specified * that allows the caller to control which files are returned in the * list. The <code>FilenameFilter</code> specified is called for each * file returned to determine whether or not that file should be included * in the list. * <p> * A <code>SecurityManager</code> check is made prior to reading the * directory. If read access to the directory is denied, an exception * will be thrown. * * @return An array of <code>File</code> objects for this directory. * * @exception SecurityException If the <code>SecurityManager</code> denies * access to this directory. * * @since 1.2 */ public File[] listFiles(FilenameFilter filter) { checkRead(); return (File[]) performList (filter, null, File.class); } /** * This method returns an array of <code>File</code> objects representing * all the files in the directory represented by this object. If this * object does not represent a directory, <code>null</code> is returned. * Each of the returned <code>File</code> object is constructed with this * object as its parent. * <p> * In this form of the <code>listFiles()</code> method, a filter is specified * that allows the caller to control which files are returned in the * list. The <code>FileFilter</code> specified is called for each * file returned to determine whether or not that file should be included * in the list. * <p> * A <code>SecurityManager</code> check is made prior to reading the * directory. If read access to the directory is denied, an exception * will be thrown. * * @return An array of <code>File</code> objects for this directory. * * @exception SecurityException If the <code>SecurityManager</code> denies * access to this directory. * * @since 1.2 */ public File[] listFiles(FileFilter filter) { checkRead(); return (File[]) performList (null, filter, File.class); } /** * 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() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -