| java.lang.Object edu.rice.cs.util.FileOps
FileOps | abstract public class FileOps (Code) | | A class to provide some convenient file operations as static methods.
It's abstract to prevent (useless) instantiation, though it can be subclassed
to provide convenient namespace importation of its methods.
version: $Id: FileOps.java 4255 2007-08-28 19:17:37Z mgricken $ |
Inner Class :public interface FileSaver | |
Inner Class :abstract public static class DefaultFileSaver implements FileSaver | |
Method Summary | |
public static String | convertToAbsolutePathEntries(String path) Convert all path entries in a path string to absolute paths. | public static void | copyFile(File source, File dest) Copies the text of one file into another. | public static File | createTempDirectory(String name) Create a new temporary directory. | public static File | createTempDirectory(String name, File parent) Create a new temporary directory. | public static boolean | deleteDirectory(File dir) Delete the given directory including any files and directories it contains.
Parameters: dir - File object representing directory to delete. | public static void | deleteDirectoryOnExit(File dir) Instructs Java to recursively delete the given directory and its contents when the JVM exits.
Parameters: dir - File object representing directory to delete. | public static File | getCanonicalFile(File f) the canonical file equivalent to f. | public static String | getCanonicalPath(File f) the canonical path for f. | public static ArrayList<File> | getFilesInDir(File d, boolean recur, FileFilter f) List all files (that is,
File s for which
isFile() is
true ) matching the provided filter in
the given directory.
Parameters: d - The directory to search. Parameters: recur - Whether subdirectories accepted by f should be recursively searched. | public static File | getValidDirectory(File origFile) Return a valid directory for use, i.e. | public static boolean | inFileTree(File f, File root) Determines whether the specified file in within the specified file tree. | public static File | makeFile(String path) | public static File | makeFile(File parentDir, String child) | public static File | makeRelativeTo(File f, File b) Makes a file equivalent to the given file f that is relative to base file b. | public static LinkedList<String> | packageExplore(String prefix, File root) | public static String | readFileAsString(File file) Reads the entire contents of a file and return them as a String. | public static String | readFileAsSwingText(File file) Reads the entire contents of a file and return them as canonicalized Swing Document text. | public static byte[] | readStreamAsBytes(InputStream stream) Reads the stream until it reaches EOF, and then returns the read contents as a byte array. | public static boolean | renameFile(File file, File dest) Renames the given file to the given destination. | public static void | saveFile(FileSaver fileSaver) This method writes files correctly; it takes care of catching errors and
making backups and keeping an unsuccessful file save from destroying the old
file (unless a backup is made). | public static String[] | splitFile(File fileToSplit) Splits a file into an array of strings representing each parent folder of the given file. | public static URL | toURL(File f) Converts the abstract pathname for f into a URL. | public static File | validate(File f) the file f unchanged if f exists; otherwise returns NULL_FILE. | public static boolean | writeIfPossible(File file, String text, boolean append) Writes the text to the given file returning true if it happend and false if it could not. | public static void | writeStringToFile(File file, String text) Writes text to the file overwriting whatever was there. | public static void | writeStringToFile(File file, String text, boolean append) Writes text to the file.
Parameters: file - File to write to Parameters: text - Text to write Parameters: append - whether to append. | public static File | writeStringToNewTempFile(String prefix, String suffix, String text) Creates a new temporary file and writes the given text to it. |
JAVA_FILE_FILTER | final public static FileFilter JAVA_FILE_FILTER(Code) | | This filter checks for files with names that end in ".java". (Note that while this filter was intended
to be a
javax.swing.filechooser.FileFilter , it actually implements a
java.io.FileFilter ,
because thats what
FileFilter means in the context of this source file.)
edu.rice.cs.plt.io.IOUtil.extensionFileFilter IOUtil.extensionFileFilter("java") |
NONEXISTENT_FILE | final public static File NONEXISTENT_FILE(Code) | | Special File object corresponding to a dummy file. Simliar to FileOption.NULL_FILE but exists() returns false.
|
convertToAbsolutePathEntries | public static String convertToAbsolutePathEntries(String path)(Code) | | Convert all path entries in a path string to absolute paths. The delimiter in the path string is the
"path.separator" property. Empty entries are equivalent to "." and will thus are converted to the
"user.dir" (working directory).
Example:
".:drjava::/home/foo/junit.jar" with "user.dir" set to "/home/foo/bar" will be converted to
"/home/foo/bar:/home/foo/bar/drjava:/home/foo/bar:/home/foo/junit.jar".
Parameters: path - path string with entries to convert path string with all entries as absolute pathsedu.rice.cs.plt.io.IOUtil.parsePathedu.rice.cs.plt.io.IOUtil.getAbsoluteFilesedu.rice.cs.plt.io.IOUtil.attemptAbsoluteFilesedu.rice.cs.plt.io.IOUtil.pathToString |
copyFile | public static void copyFile(File source, File dest) throws IOException(Code) | | Copies the text of one file into another.
Parameters: source - the file to be copied Parameters: dest - the file to be copied toedu.rice.cs.plt.io.IOUtil.copyFile |
createTempDirectory | public static File createTempDirectory(String name) throws IOException(Code) | | Create a new temporary directory. The directory will be deleted on exit, if empty.
(To delete it recursively on exit, use deleteDirectoryOnExit.)
Parameters: name - Non-unique portion of the name of the directory to create. File representing the directory that was created. |
createTempDirectory | public static File createTempDirectory(String name, File parent) throws IOException(Code) | | Create a new temporary directory. The directory will be deleted on exit, if it only contains temp files and temp
directories created after it. (To delete it on exit regardless of contents, call deleteDirectoryOnExit after
constructing the file tree rooted at this directory. Note that createTempDirectory(..) is not much more helpful
than mkdir() in this context (other than generating a new temp file name) because cleanup is a manual process.)
Parameters: name - Non-unique portion of the name of the directory to create. Parameters: parent - Parent directory to contain the new directory File representing the directory that was created. |
deleteDirectory | public static boolean deleteDirectory(File dir)(Code) | | Delete the given directory including any files and directories it contains.
Parameters: dir - File object representing directory to delete. If, for some reason, this file object is not a directory, it will still be deleted. true if there were no problems in deleting. If it returns false, something failed and the directorycontents likely at least partially still exist.edu.rice.cs.plt.io.IOUtil.deleteRecursively |
deleteDirectoryOnExit | public static void deleteDirectoryOnExit(File dir)(Code) | | Instructs Java to recursively delete the given directory and its contents when the JVM exits.
Parameters: dir - File object representing directory to delete. If, for some reason, this file object is not a directory, it will still be deleted.edu.rice.cs.plt.io.IOUtil.deleteOnExitRecursively |
getCanonicalFile | public static File getCanonicalFile(File f)(Code) | | the canonical file equivalent to f. Identical to f.getCanonicalFile() except it does not throw an exception when the file path syntax is incorrect (or an IOException or SecurityException occurs for anyother reason). It returns the absolute File intead.edu.rice.cs.plt.io.IOUtil.attemptCanonicalFile |
getCanonicalPath | public static String getCanonicalPath(File f)(Code) | | the canonical path for f. Identical to f.getCanonicalPath() except it does not throw an exception when the file path syntax is incorrect; it returns the absolute path instead.edu.rice.cs.plt.io.IOUtil.attemptCanonicalFile File String |
getFilesInDir | public static ArrayList<File> getFilesInDir(File d, boolean recur, FileFilter f)(Code) | | List all files (that is,
File s for which
isFile() is
true ) matching the provided filter in
the given directory.
Parameters: d - The directory to search. Parameters: recur - Whether subdirectories accepted by f should be recursively searched. Note that subdirectories that aren't accepted by f will be ignored. Parameters: f - The filter to apply to contained File s. An array of Files in the directory specified; if the directory does not exist, returns an empty list.edu.rice.cs.plt.io.IOUtil.attemptListFilesAsIterableedu.rice.cs.plt.io.IOUtil.listFilesRecursively(FileFileFilterFileFilter) |
getValidDirectory | public static File getValidDirectory(File origFile)(Code) | | Return a valid directory for use, i.e. one that exists and is as "close" to the file specified. It is
1) file, if file is a directory and exists
2) the closest parent of file, if file is not a directory or does not exist
3) "user.home"
a valid directory for use |
inFileTree | public static boolean inFileTree(File f, File root)(Code) | | Determines whether the specified file in within the specified file tree.
edu.rice.cs.plt.io.IOUtil.isMember null |
makeFile | public static File makeFile(String path)(Code) | | edu.rice.cs.plt.io.IOUtil.attemptCanonicalFile IOUtil.attemptCanonicalFile(new File(path)) |
makeFile | public static File makeFile(File parentDir, String child)(Code) | | edu.rice.cs.plt.io.IOUtil.attemptCanonicalFile IOUtil.attemptCanonicalFile(new File(parentDir, child)) |
makeRelativeTo | public static File makeRelativeTo(File f, File b) throws IOException, SecurityException(Code) | | Makes a file equivalent to the given file f that is relative to base file b. In other words,
new File(b,makeRelativeTo(base,abs)).getCanonicalPath() equals
f.getCanonicalPath()
In Linux/Unix, if the file f is /home/username/folder/file.java and the file b is
/home/username/folder/sublevel/file2.java , then the resulting File path from this method would be
../file.java while its canoncial path would be /home/username/folder/file.java .
Warning: this method is inherently broken, because it assumes a relative path exists between all
files. The Java file system model, however, supports multiple system roots (see
File.listRoots ).
Thus, two files from different "file systems" (in Windows, different drives) have no common parent.
Parameters: f - The path that is to be made relative to the base file Parameters: b - The file to make the next file relative to A new file whose path is relative to the base file while the value of getCanonicalPath() for the returned file is the same as the result of getCanonicalPath() for the given file. |
packageExplore | public static LinkedList<String> packageExplore(String prefix, File root)(Code) | | This function starts from the given directory and finds all packages within that directory
Parameters: prefix - the package name of files in the given root Parameters: root - the directory to start exploring from a list of valid packages, excluding the root ("") package |
readFileAsString | public static String readFileAsString(File file) throws IOException(Code) | | Reads the entire contents of a file and return them as a String.
edu.rice.cs.plt.io.IOUtil.toString(File) |
readFileAsSwingText | public static String readFileAsSwingText(File file) throws IOException(Code) | | Reads the entire contents of a file and return them as canonicalized Swing Document text. All newLine sequences,
including "\n", "\r", and "\r\n" are converted to "\n".
|
readStreamAsBytes | public static byte[] readStreamAsBytes(InputStream stream) throws IOException(Code) | | Reads the stream until it reaches EOF, and then returns the read contents as a byte array. This call may
block, since it will not return until EOF has been reached.
Parameters: stream - Input stream to read. Byte array consisting of all data read from stream.edu.rice.cs.plt.io.IOUtil.toByteArray IOUtil InputStream |
renameFile | public static boolean renameFile(File file, File dest)(Code) | | Renames the given file to the given destination. Needed since Windows will not allow a rename to
overwrite an existing file.
Parameters: file - the file to rename Parameters: dest - the destination file true iff the rename was successfuledu.rice.cs.plt.io.IOUtil.attemptMove |
saveFile | public static void saveFile(FileSaver fileSaver) throws IOException(Code) | | This method writes files correctly; it takes care of catching errors and
making backups and keeping an unsuccessful file save from destroying the old
file (unless a backup is made). It makes sure that the file to be saved is
not read-only, throwing an IOException if it is. Note: if saving fails and a
backup was being created, any existing backup will be destroyed (this is
because the backup is written before saving begins, and then moved back over
the original file when saving fails). As the old backup would have been destroyed
anyways if saving had succeeded, I do not think that this is incorrect or
unreasonable behavior.
Parameters: fileSaver - keeps track of the name of the file to write, whether to back up the file, and has a method that actually performs the writing of the file throws: IOException - if the saving or backing up of the file fails for any reason |
splitFile | public static String[] splitFile(File fileToSplit)(Code) | | Splits a file into an array of strings representing each parent folder of the given file. The file whose path
is /home/username/txt.txt in linux would be split into the string array:
{"","home","username","txt.txt"}. Delimeters are excluded.
Parameters: fileToSplit - the file to split into its directories.edu.rice.cs.plt.io.IOUtil.fullPath File |
toURL | public static URL toURL(File f) throws MalformedURLException(Code) | | Converts the abstract pathname for f into a URL. This method is included in class java.io.File as f.toURL(), but
has been deprecated in Java 6.0 because escape characters on some systems are not handled correctly. The workaround,
f.toURI().toURL(), is unsatisfactory because we rely on the old (broken) behavior: toURI() produces escape
characters (for example, " " becomes "%20"), which remain in the name when we attempt to convert back
to a filename. That is, f.toURI().toURL().getFile() may not be a valid path, even if f exists. (The correct
solution is to avoid trying to convert from a URL to a File, because this conversion is not guaranteed
to work.)
|
validate | public static File validate(File f)(Code) | | the file f unchanged if f exists; otherwise returns NULL_FILE. |
writeIfPossible | public static boolean writeIfPossible(File file, String text, boolean append)(Code) | | Writes the text to the given file returning true if it happend and false if it could not. This is a
simple wrapper for writeStringToFile that doesn't throw an IOException.
Parameters: file - File to write to Parameters: text - Text to write Parameters: append - whether to append. (false=overwrite)edu.rice.cs.plt.io.IOUtil.attemptWriteStringToFile(FileStringboolean) |
writeStringToFile | public static void writeStringToFile(File file, String text) throws IOException(Code) | | Writes text to the file overwriting whatever was there.
Parameters: file - File to write to Parameters: text - Test to writeedu.rice.cs.plt.io.IOUtil.writeStringToFile(FileString) |
writeStringToFile | public static void writeStringToFile(File file, String text, boolean append) throws IOException(Code) | | Writes text to the file.
Parameters: file - File to write to Parameters: text - Text to write Parameters: append - whether to append. (false=overwrite)edu.rice.cs.plt.io.IOUtil.writeStringToFile(FileStringboolean) |
writeStringToNewTempFile | public static File writeStringToNewTempFile(String prefix, String suffix, String text) throws IOException(Code) | | Creates a new temporary file and writes the given text to it. The file will be deleted on exit.
Parameters: prefix - Beginning part of file name, before unique number Parameters: suffix - Ending part of file name, after unique number Parameters: text - Text to write to file name of the temporary file that was creatededu.rice.cs.plt.io.IOUtil.createAndMarkTempFile(StringString)edu.rice.cs.plt.io.IOUtil.writeStringToFile(FileString) |
|
|