org.opensha.commons.util
Class FileUtils

java.lang.Object
  extended by org.opensha.commons.util.FileUtils

public class FileUtils
extends Object

Title:FileUtils

Description:Generic functions used in handling text files, such as loading in the text data from a file.

Version:
1.0
Author:
Steven W. Rock

Field Summary
protected static String C
          Class name used for debug strings
protected static boolean D
          boolean that indicates if print out debug statements
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copyInputStream(InputStream in, OutputStream out)
           
static File createTempDir()
           
static void createZipFile(String filesPath)
          This function creates a Zip file called "allFiles.zip" for all the files that exist in filesPath.
static void createZipFile(String zipFile, String dir, Collection<String> fileNames)
          This creates a zip file with the given name from a list of file names.
static boolean deleteRecursive(File f)
           
static void downloadURL(String addr, File outFile)
           
static void downloadURL(URL url, File outFile)
           
static ArrayList<String> loadFile(String fileName)
          Loads in each line to a text file into an ArrayList ( i.e.
static ArrayList<String> loadFile(String fileName, boolean skipBlankLines)
          Loads in each line to a text file into an ArrayList ( i.e.
static ArrayList<String> loadFile(URL url)
           
static Image loadImage(String fileName)
          this method accepts the filename and loads the image from the jar file
static ArrayList<String> loadJarFile(String fileName)
          load from Jar file
static Object loadObject(String fileName)
           
static Object loadObjectFromURL(URL url)
          return a object read from the URL
static ArrayList<String> loadStream(InputStream is)
           
static void print(PrintJob pjob, Graphics pg, String textToPrint)
          Prints a Text file
static void save(String fileName, String dataToSave)
          Saves the text to a file on the users machine.
static void saveObjectInFile(String fileName, Object obj)
          save the serialized object into the specified file
static void unzipFile(File zipFile, File directory)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

C

protected static final String C
Class name used for debug strings

See Also:
Constant Field Values

D

protected static final boolean D
boolean that indicates if print out debug statements

See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

loadFile

public static ArrayList<String> loadFile(String fileName)
                                  throws FileNotFoundException,
                                         IOException
Loads in each line to a text file into an ArrayList ( i.e. a vector ). Each element in the ArrayList represents one line from the file.

Parameters:
fileName - File to load in
Returns:
ArrayList each element one line from the file
Throws:
FileNotFoundException - If the filename doesn't exist
IOException - Unable to read from the file

loadFile

public static ArrayList<String> loadFile(String fileName,
                                         boolean skipBlankLines)
                                  throws FileNotFoundException,
                                         IOException
Loads in each line to a text file into an ArrayList ( i.e. a vector ). Each element in the ArrayList represents one line from the file.

Parameters:
fileName - File to load in
Returns:
ArrayList each element one line from the file
Throws:
FileNotFoundException - If the filename doesn't exist
IOException - Unable to read from the file

loadFile

public static ArrayList<String> loadFile(URL url)
                                  throws IOException
Parameters:
url - : URL of file to be read
Returns:
: arrayList containing the lines in file
Throws:
FileNotFoundException
IOException

loadStream

public static ArrayList<String> loadStream(InputStream is)
                                    throws IOException
Parameters:
is - : input stream of file to be read
Returns:
: arrayList containing the lines in file
Throws:
FileNotFoundException
IOException

loadJarFile

public static ArrayList<String> loadJarFile(String fileName)
                                     throws FileNotFoundException,
                                            IOException
load from Jar file

Parameters:
fileName - : File name to be read from Jar file
Returns:
Throws:
FileNotFoundException
IOException

saveObjectInFile

public static void saveObjectInFile(String fileName,
                                    Object obj)
                             throws IOException
save the serialized object into the specified file

Parameters:
fileName -
obj -
Throws:
IOException

loadObjectFromURL

public static Object loadObjectFromURL(URL url)
return a object read from the URL

Parameters:
url -
Returns:

createZipFile

public static void createZipFile(String zipFile,
                                 String dir,
                                 Collection<String> fileNames)
                          throws IOException
This creates a zip file with the given name from a list of file names.

Parameters:
zipFile -
files -
Throws:
IOException

createZipFile

public static void createZipFile(String filesPath)
This function creates a Zip file called "allFiles.zip" for all the files that exist in filesPath.

Parameters:
filesPath - String Folder with absolute path in zip file will be created. This function searches for all the files in the folder "filesPath" and adds those to a single zip file "allFiles.zip".

unzipFile

public static void unzipFile(File zipFile,
                             File directory)
                      throws FileNotFoundException,
                             IOException
Throws:
FileNotFoundException
IOException

copyInputStream

public static final void copyInputStream(InputStream in,
                                         OutputStream out)
                                  throws IOException
Throws:
IOException

loadObject

public static Object loadObject(String fileName)
Parameters:
fileName - File from where object needs to be read
Returns:
Object object read from the file

createTempDir

public static File createTempDir()
                          throws IOException
Throws:
IOException

deleteRecursive

public static boolean deleteRecursive(File f)

loadImage

public static Image loadImage(String fileName)
this method accepts the filename and loads the image from the jar file

Parameters:
fileName -
Returns:

downloadURL

public static void downloadURL(String addr,
                               File outFile)
                        throws IOException
Throws:
IOException

downloadURL

public static void downloadURL(URL url,
                               File outFile)
                        throws IOException
Throws:
IOException

print

public static void print(PrintJob pjob,
                         Graphics pg,
                         String textToPrint)
Prints a Text file

Parameters:
pjob - PrintJob created using getToolkit().getPrintJob(JFrame,String,Properties);
pg - Graphics
textToPrint - String

save

public static void save(String fileName,
                        String dataToSave)
Saves the text to a file on the users machine. File is saved with extension ".txt".

Parameters:
panel - Component
dataToSave - String