📄 file.java
字号:
* complete path of the file after the last instance of the separator * string. * * @return The file name */ public String getName() { int pos = PlatformHelper.lastIndexOfSeparator (path); if (pos == -1) return path; if (PlatformHelper.endWithSeparator (path)) return ""; return path.substring (pos + separator.length()); } /** * 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() { if (PlatformHelper.isRootDirectory (path)) return null; String par_path = path; int pos = PlatformHelper.lastIndexOfSeparator (par_path); if (pos == -1) return null; return par_path.substring (0, pos); } /** * 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. * <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 boolean isAbsolute() { if (PlatformHelper.beginWithRootPathPrefix (path) > 0) return true; else return false; } /** * 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 isDirectoryInternal (path); } /** * 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 isFileInternal (path); } /** * 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() { // FIXME: this only works on UNIX if (getName().startsWith(".")) return true; else return false; } /* * This native method does the actual work of getting the last file * modification time. It also does the existence check to avoid the * overhead of a call to exists() */ private native long lastModifiedInternal(String path); /** * 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 lastModifiedInternal (path); } /* * This native method actually determines the length of the file and * handles the existence check */ private native long lengthInternal(String path); /** * 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 lengthInternal (path); } /* * This native function actually produces the list of file in this * directory */ private native String[] listInternal (String dirname); /** * 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(); // Get the list of files String list_path = PlatformHelper.removeTailSeparator(path); File dir = new File(list_path); if (! dir.exists() || ! dir.isDirectory() ) return null; String files[] = listInternal(list_path); if (files == null) return new String[0]; if (filter == null) return files; // Apply the filter int count = 0; for (int i = 0; i < files.length; i++) { if (filter.accept(this, files[i])) ++count; else files[i] = null; } String[] retfiles = new String[count]; count = 0; for (int i = 0; i < files.length; i++) if (files[i] != null) retfiles[count++] = files[i]; return retfiles; } /** * 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() { return list (null); } /** * 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() { return listFiles ((FilenameFilter) null); } /** * 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) { String[] filelist = list (filter); if (filelist == null) return null; File[] fobjlist = new File [filelist.length]; for (int i = 0; i < filelist.length; i++) fobjlist [i] = new File (this, filelist [i]); return fobjlist; } /** * 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) { File[] fobjlist = listFiles ((FilenameFilter) null); if (fobjlist == null) return null; if (filter == null) return fobjlist; 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; } /** * 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 { String abspath = getAbsolutePath(); try { if (new File (abspath).isDirectory()) abspath = abspath + separator; } catch(Exception _) { } String url_string = "file://" + abspath; return new URL (url_string); } /** * @since 1.4 * @author Dalibor Topic */ public URI toURI() { try { return new URI("file", null, (isDirectory() ? getAbsolutePath() + separator : getAbsolutePath()), null, null); } catch (URISyntaxException e) { throw (IllegalArgumentException) new IllegalArgumentException("Couldn't convert "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -