📄 file.java
字号:
/* File.java -- Class representing a file on disk
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.io;
import gnu.java.io.PlatformHelper;
import java.net.MalformedURLException;
import java.net.URL;
/**
* This class represents a file or directory on a local disk. It provides
* facilities for dealing with a variety of systems that use various
* types of path separators ("/" versus "\", for example). It also
* contains method useful for creating and deleting files and directories.
*
* @version 0.0
*
* @author Aaron M. Renn (arenn@urbanophile.com)
* @author E. Prangsma (connection to JNode)
*/
public class File implements Serializable, Comparable {
static final long serialVersionUID = 301077366599181567L;
/*************************************************************************/
/*
* Class Variables
*/
/**
* This is the path separator string for the current host. This field
* contains the value of the <code>file.separator</code> system property.
* An example separator string would be "/" on the GNU system.
*/
public static final String separator = System.getProperty("file.separator");
/**
* This is the first character of the file separator string. On many
* hosts (for example, on the GNU system), this represents the entire
* separator string. The complete separator string is obtained from the
* <code>file.separator</code>system property.
*/
public static final char separatorChar = separator.charAt(0);
/**
* This is the string that is used to separate the host name from the
* path name in paths than include the host name. It is the value of
* the <code>path.separator</code> system property.
*/
public static final String pathSeparator =
System.getProperty("path.separator");
/**
* This is the first character of the string used to separate the host name
* from the path name in paths that include a host. The separator string
* is taken from the <code>path.separator</code> system property.
*/
public static final char pathSeparatorChar = pathSeparator.charAt(0);
/*************************************************************************/
/*
* Instance Variables
*/
/**
* This is the path to the file set when the object is created. It
* may be an absolute or relative path name.
*/
private String path;
/*************************************************************************/
/*
* Class Methods
*/
/**
* 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 IllegalArgumentException, SecurityException, IOException {
return (createTempFile(prefix, suffix, null));
}
/*************************************************************************/
/**
* 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
*/
public static synchronized File createTempFile(
String prefix,
String suffix,
File directory)
throws IllegalArgumentException, SecurityException, 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.");
}
// Now process the prefix and suffix.
if (prefix.length() < 3)
throw new IllegalArgumentException("Prefix too short: " + prefix);
if (suffix == null)
suffix = ".tmp";
// Now identify a file name and make sure it doesn't exist
File f;
for (;;) {
String filename = prefix + System.currentTimeMillis() + suffix;
f = new File(directory, filename);
if (f.exists())
continue;
else
break;
}
// Verify that we are allowed to create this file
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkWrite(f.getAbsolutePath());
// Now create the file and return our file object
createInternal(f.getAbsolutePath());
return (f);
}
/*************************************************************************/
/**
* This method is used to create a temporary file
*/
private static boolean createInternal(String name) throws IOException {
return false;
}
/*************************************************************************/
/**
* 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.
*/
public static File[] listRoots() {
try {
return VMIOUtils.getAPI().getRoots();
} catch (IOException ex) {
return new File[0];
}
}
/*************************************************************************/
/*
* Constructors
*/
/**
* This method initializes a new <code>File</code> object to represent
* a file in the specified directory. If the <code>directory</code>
* argument is <code>null</code>, the file is assumed to be in the
* current directory as specified by the <code>user.dir</code> system
* property
*
* @param directory The directory this file resides in
* @param name The name of the file
*/
public File(File directory, String name) {
if (directory == null) {
String dirname = System.getProperty("user.dir");
if (dirname == null)
throw new IllegalArgumentException("Cannot determine default user directory");
directory = new File(dirname);
}
String dirpath = directory.getPath();
if (PlatformHelper.isRootDirectory(dirpath))
path = dirpath + name;
else
path = dirpath + separator + name;
}
/*************************************************************************/
/**
* This method initializes a new <code>File</code> object to represent
* a file in the specified named directory. The path name to the file
* will be the directory name plus the separator string plus the file
* name. If the directory path name ends in the separator string, another
* separator string will still be appended.
*
* @param dirname The path to the directory the file resides in
* @param name The name of the file
*/
public File(String dirname, String name) {
this(dirname == null ? (File) null : new File(dirname), name);
}
/*************************************************************************/
/**
* This method initializes a new <code>File</code> object to represent
* a file with the specified path.
*
* @param name The path name of the file
*/
public File(String name) {
path = name;
// Per the spec
if (path == null)
throw new NullPointerException("File name is null");
if (!PlatformHelper.isRootDirectory(path))
while (PlatformHelper.endWithSeparator(path))
path = PlatformHelper.removeTailSeparator(path);
}
/*************************************************************************/
/*
* Instance Methods
*/
/**
* 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 pos = PlatformHelper.lastIndexOfSeparator(path);
if (pos == -1)
return (path);
if (PlatformHelper.endWithSeparator(path))
return ("");
return (path.substring(pos + separator.length()));
}
/*************************************************************************/
/**
* 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 the path of this file as an absolute path name.
* If the path name is already absolute, then it is returned. Otherwise
* the value returned is the current directory plus the separatory
* string plus the path of the file. The current directory is determined
* from the <code>user.dir</code> system property.
*
* @return The absolute path of this file
*/
public String getAbsolutePath() {
if (isAbsolute())
return path;
String dir = System.getProperty("user.dir");
if (dir == null)
return path;
if (PlatformHelper.endWithSeparator(dir))
return dir + path;
return dir + separator + 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.
*/
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 String getCanonicalPath() throws IOException {
String abspath = getAbsolutePath();
return PlatformHelper.toCanonicalForm(abspath);
}
/*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -