Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    Contains static utilities for manipulating files. Created: Thu Nov 20 15:14:16 2003
    Version:
    $Id: FileUtils.java,v 1.1 2007/10/22 21:37:40 mccallum Exp $
    Author:
    Charles Sutton
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] readFile​(java.io.File f)
      Reads every line from a given text file.
      static java.lang.Object readGzippedObject​(java.io.File f)
      Reads a serialized object from a file that has been compressed using gzip.
      static java.lang.Object readObject​(java.io.File f)
      Reads a Serialized object, which may or may not be zipped.
      static java.lang.Object readUnzippedObject​(java.io.File f)
      Reads a serialized object from a file.
      static java.io.File uniqueFile​(java.io.File dir, java.lang.String prefix, java.lang.String extension)
      Creates a file, making sure that its name is unique.
      static void writeGzippedObject​(java.io.File f, java.io.Serializable obj)
      Writes a serialized version of obj to a given file, compressing it using gzip.
      static void writeObject​(java.io.File f, java.io.Serializable obj)
      Serializes an object to a file, masking out annoying exceptions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readObject

        public static java.lang.Object readObject​(java.io.File f)
        Reads a Serialized object, which may or may not be zipped. Guesses from the file name whether to decompress or not.
        Parameters:
        f - File to read data from
        Returns:
        A deserialized object.
      • readUnzippedObject

        public static java.lang.Object readUnzippedObject​(java.io.File f)
        Reads a serialized object from a file. You probably want to use readObject(java.io.File) instead, because that method will automatically guess from the extension whether the file is compressed, and call this method if necessary.
        Parameters:
        f - File to read object from
        See Also:
        readObject(java.io.File)
      • readFile

        public static java.lang.String[] readFile​(java.io.File f)
                                           throws java.io.IOException
        Reads every line from a given text file.
        Parameters:
        f - Input file.
        Returns:
        String[] Array containing each line in f.
        Throws:
        java.io.IOException
      • uniqueFile

        public static java.io.File uniqueFile​(java.io.File dir,
                                              java.lang.String prefix,
                                              java.lang.String extension)
                                       throws java.io.IOException
        Creates a file, making sure that its name is unique. The file will be created as if by new File(dir, prefix+i+extension), where i is an integer chosen such that the returned File does not exist.
        Parameters:
        dir - Directory to use for the returned file
        prefix - Prefix of the file name (before the uniquifying integer)
        extension - Suffix of the file name (after the uniquifying integer)
        Throws:
        java.io.IOException
      • writeGzippedObject

        public static void writeGzippedObject​(java.io.File f,
                                              java.io.Serializable obj)
        Writes a serialized version of obj to a given file, compressing it using gzip.
        Parameters:
        f - File to write to
        obj - Object to serialize
      • readGzippedObject

        public static java.lang.Object readGzippedObject​(java.io.File f)
        Reads a serialized object from a file that has been compressed using gzip. You probably want to use readObject(java.io.File) instead, because it will automatically guess from the extension whether the file is compressed, and call this method if necessary.
        Parameters:
        f - Compressed file to read object from
        See Also:
        readObject(java.io.File)