com.javaranch.common
Class Files

java.lang.Object
  |
  +--com.javaranch.common.Files

public class Files
extends java.lang.Object

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

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton, Carl Trusiak

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

getResourceAsString

public static java.lang.String getResourceAsString(java.lang.Object obj,
                                                   java.lang.String filename)
                                            throws java.lang.Exception
Pass in an object and a relative filename and the file will be opened relative to where the class came from.

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" );

Parameters:
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.

Returns:
null if the file cannot be loaded.

java.lang.Exception

fileToVector

public static java.util.Vector fileToVector(java.lang.String fileName)
                                     throws java.io.IOException
Deprecated.  

All of a text file is read in and stored in a Vector.

Parameters:
fileName - The name of the text file to read in.

Returns:
The Vector where each object in the Vector contains one line from the text file.

Throws:
java.io.IOException -


fileToList

public static java.util.List fileToList(java.lang.String fileName)
                                 throws java.io.IOException
All of a text file is read in and stored in a Vector.

Parameters:
fileName - The name of the text file to read in.

Returns:
The Vector where each object in the Vector contains one line from the text file.

Throws:
java.io.IOException -


fileToArray

public static java.lang.String[] fileToArray(java.lang.String fileName)
                                      throws java.io.IOException
All of a text file is read in and stored in a String array.

Parameters:
fileName - The name of the text file to read in.

Returns:
The String array where each String contains one line from the text file.

Throws:
java.io.IOException -


arrayToFile

public static void arrayToFile(java.lang.String[] s,
                               java.lang.String fileName)
                        throws java.io.IOException
Replaced with toTextFile().

java.io.IOException

toTextFile

public static void toTextFile(java.util.List list,
                              java.lang.String fileName)
                       throws java.io.IOException
Each element of a List has the toString() method performed on it and written to a text file.

Parameters:
list - The collection of objects to be written out.

fileName - The name of the text file to be created.

java.io.IOException

toTextFile

public static void toTextFile(java.lang.String[] s,
                              java.lang.String fileName)
                       throws java.io.IOException
Each element of a string array is written to a text file.

Parameters:
s - The collection of objects to be written out.

fileName - The name of the text file to be created.

java.io.IOException

delete

public static void delete(java.lang.String fileName)
Delete a specific file.

If the file does not exist, nothing is done.


deleteAllFilesInDir

public static void deleteAllFilesInDir(java.lang.String directoryName)
Delete a specific file.

All subdirectories are ignored.


rename

public static void rename(java.lang.String source,
                          java.lang.String dest)
Change the file name.

If dest already exists, it is deleted.


writeObject

public static boolean writeObject(java.lang.String fileName,
                                  java.lang.Object obj)
Write one object to a file.

If the file already exists, it is overwritten.

Returns:
true if file write successful.


readObject

public static java.lang.Object readObject(java.lang.String fileName)
Read one object from a file.

Returns:
object or null if file read unsuccessful.


fileToByteArrayE

public static byte[] fileToByteArrayE(java.lang.String filename)
                               throws java.io.IOException
Read a file from the disk into a byte array.

Will throw an IOException if one is encountered.

Parameters:
filename - The name of the file to read.

Returns:
A byte array that is the exact same size as the file.

Throws:
java.io.IOException

fileToByteArray

public static byte[] fileToByteArray(java.lang.String filename)
Read a file from the disk into a byte array.

If an IOException is encountered, a message is printed to STDOUT and null is returned.

Parameters:
filename - The name of the file to read.

Returns:
A byte array that is the exact same size as the file.


byteArrayToFile

public static void byteArrayToFile(byte[] data,
                                   java.lang.String filename)
                            throws java.io.IOException
java.io.IOException

getDirectory

public static java.lang.String[] getDirectory(java.io.File f)
Return a string array containing the current directory.

Directories will have angle brackets.

If there is a parent directory, the first entry will be a .. directory.


getAllFilesInDirectory

public 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.

If an invalid directory is passed in, or if the directory contains no valid files, an array of length zero is returned.


deleteDirectory

public static void deleteDirectory(java.io.File dir)
                            throws java.io.IOException
Deletes a directory structure.

Parameters:
dir - Directory to delete.
java.io.IOException

getDirectoryDetails

public static java.lang.String[] getDirectoryDetails(java.io.File f)
Return a string array containing the current directory with timestamps and file sizes.

Directories will have angle brackets.

If there is a parent directory, the first entry will be a .. directory.


copy

public static void copy(java.io.InputStream in,
                        java.io.OutputStream out)
                 throws java.lang.Exception
java.lang.Exception

appendText

public static boolean appendText(java.lang.String filename,
                                 java.lang.String text)

fileToUniqueArray

public static java.lang.String[] fileToUniqueArray(java.lang.String FileName)
                                            throws java.io.IOException
All of a text file is read in and unique Strings are stored in a String array.

Parameters:
FileName - The name of the text file to read in.

Returns:
The String array where each String contains one unique line from the text file.

Throws:
java.io.IOException -


fileToHashSet

public static java.util.HashSet fileToHashSet(java.lang.String FileName)
                                       throws java.io.IOException
All of a text file is read in and unique elements are stored in a HashSet.

Parameters:
FileName - The name of the text file to read in.

Returns:
The HashSet where each element contains one unique line from the text file.

Throws:
java.io.IOException -



Copyright ©2004 Paul Wheaton All Rights Reserved