|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.javaranch.common.Files
Contains a collection of static methods related to files.
- - - - - - - - - - - - - - - - -
Copyright (c) 1998-2004 Paul Wheaton
You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...
My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.
The original source can be found at JavaRanch
- - - - - - - - - - - - - - - - -
Method Summary | |
static boolean |
appendText(java.lang.String filename,
java.lang.String text)
|
static void |
arrayToFile(java.lang.String[] s,
java.lang.String fileName)
Replaced with toTextFile(). |
static void |
byteArrayToFile(byte[] data,
java.lang.String filename)
|
static void |
copy(java.io.InputStream in,
java.io.OutputStream out)
|
static void |
delete(java.lang.String fileName)
Delete a specific file. |
static void |
deleteAllFilesInDir(java.lang.String directoryName)
Delete a specific file. |
static void |
deleteDirectory(java.io.File dir)
Deletes a directory structure. |
static java.lang.String[] |
fileToArray(java.lang.String fileName)
All of a text file is read in and stored in a String array. |
static byte[] |
fileToByteArray(java.lang.String filename)
Read a file from the disk into a byte array. |
static byte[] |
fileToByteArrayE(java.lang.String filename)
Read a file from the disk into a byte array. |
static java.util.HashSet |
fileToHashSet(java.lang.String FileName)
All of a text file is read in and unique elements are stored in a HashSet. |
static java.util.List |
fileToList(java.lang.String fileName)
All of a text file is read in and stored in a Vector. |
static java.lang.String[] |
fileToUniqueArray(java.lang.String FileName)
All of a text file is read in and unique Strings are stored in a String array. |
static java.util.Vector |
fileToVector(java.lang.String fileName)
Deprecated. |
static java.lang.String[] |
getAllFilesInDirectory(java.lang.String directory)
Return a string array containing the names of all the files (not directories) in the current directory. |
static java.lang.String[] |
getDirectory(java.io.File f)
Return a string array containing the current directory. |
static java.lang.String[] |
getDirectoryDetails(java.io.File f)
Return a string array containing the current directory with timestamps and file sizes. |
static java.lang.String |
getResourceAsString(java.lang.Object obj,
java.lang.String filename)
Pass in an object and a relative filename and the file will be opened relative to where the class came from. |
static java.lang.Object |
readObject(java.lang.String fileName)
Read one object from a file. |
static void |
rename(java.lang.String source,
java.lang.String dest)
Change the file name. |
static void |
toTextFile(java.util.List list,
java.lang.String fileName)
Each element of a List has the toString() method performed on it and written to a text file. |
static void |
toTextFile(java.lang.String[] s,
java.lang.String fileName)
Each element of a string array is written to a text file. |
static boolean |
writeObject(java.lang.String fileName,
java.lang.Object obj)
Write one object to a file. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static java.lang.String getResourceAsString(java.lang.Object obj, java.lang.String filename) throws java.lang.Exception
Suppose you have a class called com.cows.Moo. And in the constructor you want to load your essay on cows conveniently located at com/cows/data/mooing_cow.txt. This file sits in the subdirectory which sits in the same directory as the com.cows.Moo class! But is your class being used from a jar file? Over the internet? Worry no more! This method will load your file through the same pipe that the class was loaded. In this example, call getResourceAsString( this , "data/mooing_cow.txt" );
obj
- The address for the file you are looking for is relative to this object. filename
- The address for the file you are looking for relative to the given object.
java.lang.Exception
public static java.util.Vector fileToVector(java.lang.String fileName) throws java.io.IOException
fileName
- The name of the text file to read in.
java.io.IOException
- public static java.util.List fileToList(java.lang.String fileName) throws java.io.IOException
fileName
- The name of the text file to read in.
java.io.IOException
- public static java.lang.String[] fileToArray(java.lang.String fileName) throws java.io.IOException
fileName
- The name of the text file to read in.
java.io.IOException
- public static void arrayToFile(java.lang.String[] s, java.lang.String fileName) throws java.io.IOException
java.io.IOException
public static void toTextFile(java.util.List list, java.lang.String fileName) throws java.io.IOException
list
- The collection of objects to be written out. fileName
- The name of the text file to be created.
java.io.IOException
public static void toTextFile(java.lang.String[] s, java.lang.String fileName) throws java.io.IOException
s
- The collection of objects to be written out. fileName
- The name of the text file to be created.
java.io.IOException
public static void delete(java.lang.String fileName)
If the file does not exist, nothing is done.
public static void deleteAllFilesInDir(java.lang.String directoryName)
All subdirectories are ignored.
public static void rename(java.lang.String source, java.lang.String dest)
If dest already exists, it is deleted.
public static boolean writeObject(java.lang.String fileName, java.lang.Object obj)
If the file already exists, it is overwritten.
public static java.lang.Object readObject(java.lang.String fileName)
public static byte[] fileToByteArrayE(java.lang.String filename) throws java.io.IOException
Will throw an IOException if one is encountered.
filename
- The name of the file to read.
java.io.IOException
public static byte[] fileToByteArray(java.lang.String filename)
If an IOException is encountered, a message is printed to STDOUT and null is returned.
filename
- The name of the file to read.
public static void byteArrayToFile(byte[] data, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static java.lang.String[] getDirectory(java.io.File f)
Directories will have angle brackets.
If there is a parent directory, the first entry will be a .. directory.
public static java.lang.String[] getAllFilesInDirectory(java.lang.String directory)
If an invalid directory is passed in, or if the directory contains no valid files, an array of length zero is returned.
public static void deleteDirectory(java.io.File dir) throws java.io.IOException
dir
- Directory to delete.
java.io.IOException
public static java.lang.String[] getDirectoryDetails(java.io.File f)
Directories will have angle brackets.
If there is a parent directory, the first entry will be a .. directory.
public static void copy(java.io.InputStream in, java.io.OutputStream out) throws java.lang.Exception
java.lang.Exception
public static boolean appendText(java.lang.String filename, java.lang.String text)
public static java.lang.String[] fileToUniqueArray(java.lang.String FileName) throws java.io.IOException
FileName
- The name of the text file to read in.
java.io.IOException
- public static java.util.HashSet fileToHashSet(java.lang.String FileName) throws java.io.IOException
FileName
- The name of the text file to read in.
java.io.IOException
-
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |