📄 file.java
字号:
/**
* 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() throws SecurityException {
// Check the SecurityManager
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkWrite(path);
String mk_path;
mk_path = PlatformHelper.removeTailSeparator(path);
return (mkdirInternal(mk_path));
}
/*************************************************************************/
/**
* This native method actually creates the directory
*/
private boolean mkdirInternal(String path) {
String absolute = PlatformHelper.toCanonicalForm(getAbsolutePath());
// TODO
return false;
}
/*************************************************************************/
/**
* 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() throws SecurityException {
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 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) throws SecurityException {
// Check the SecurityManager
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkWrite(path);
// Call our native rename method
boolean rc = renameToInternal(path, dest.getPath());
return (rc);
}
/*************************************************************************/
/**
* This native method actually performs the rename.
*/
private boolean renameToInternal(String target, String dest) {
return false;
}
/*************************************************************************/
/**
* 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 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) {
// Check the SecurityManager
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkRead(path);
}
// Get the list of files
final String list_path = PlatformHelper.removeTailSeparator(path);
final File dir = new File(list_path);
if (!dir.exists() || !dir.isDirectory()) {
return null;
}
try {
final String files[] = VMIOUtils.getAPI().list(dir, filter);
if (files == null) {
return new String[0];
} else {
return files;
}
} catch (IOException ex) {
return 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.
*/
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.
*/
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.
*/
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 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>
*/
public int compareTo(Object obj) throws ClassCastException {
return (compareTo((File)obj));
}
/*************************************************************************/
/**
* 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
*/
public int compareTo(File file) {
String p1, p2;
try {
p1 = getCanonicalPath();
p2 = file.getCanonicalPath();
} catch (IOException e) {
// 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 tests two <code>File</code> objects for equality by
* comparing the path of the specified <code>File</code> against the path
* of this object. The two objects are equal if an only if 1) The
* argument is not null 2) The argument is a <code>File</code> object and
* 3) The path of the <code>File</code>argument is equal to the path
* of this object.
* <p>
* The paths of the files are determined by calling the <code>getPath()</code>
* method on each object.
*
* @return <code>true</code> if the two objects are equal, <code>false</code> otherwise.
*/
public boolean equals(Object obj) {
if (obj == null)
return (false);
if (!(obj instanceof File))
return (false);
File f = (File)obj;
return (f.getPath().equals(getPath()));
}
/*************************************************************************/
/**
* 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() {
return (getPath().hashCode() ^ 1234321);
}
/*************************************************************************/
/**
* 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));
}
} // class File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -