com.croftsoft.core.io
Class FileLib

java.lang.Object
  extended by com.croftsoft.core.io.FileLib

public final class FileLib
extends Object

A library of static methods to manipulate File objects.

Since:
1999-08-15
Version:
2001-06-10
Author:
David Wallace Croft

Method Summary
static File findFile(String dirname, String filename, boolean ignoreFilenameCase)
          Returns null if the file does not already exist.
static String getExtension(File file)
          Parses out the file name extension.
static String loadTextFile(String filename)
          Loads the text file content into a String.
static void main(String[] args)
          Test method.
static boolean makeParents(String pathFilename)
          Creates the parent directories for a file specified by a combined path and filename String.
static String pareExtension(String fileName)
          Strips off the file name extension.
static boolean replaceBytes(File file, byte[] oldBytes, byte[] newBytes)
          Replaces instances of oldBytes with newBytes in the inFile.
static boolean replaceBytes(File inFile, File outFile, byte[] oldBytes, byte[] newBytes)
          Copies inFile to outFile with the replacement of occurrences of oldBytes with newBytes.
static boolean replaceStrings(File inFile, File outFile, String oldString, String newString)
          Copies inFile to outFile with the replacement of occurrences of oldString with newString.
static boolean replaceStrings(File file, String oldString, String newString)
          Replaces instances of oldString with newString in the inFile.
static void saveTextFile(String filename, String text)
          Saves text to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

main

public static void main(String[] args)
Test method.


findFile

public static File findFile(String dirname,
                            String filename,
                            boolean ignoreFilenameCase)
Returns null if the file does not already exist.


getExtension

public static String getExtension(File file)
Parses out the file name extension.

Returns:
Returns null if file name does not have a period;
returns the empty String ("") if the file name has only one period and it is the last character;
otherwise returns everything after the first period.

makeParents

public static boolean makeParents(String pathFilename)
Creates the parent directories for a file specified by a combined path and filename String. The name after the last separator in pathFilename is not created as a directory and is typically a filename. Used when the normal File.mkdirs() operation would mistakenly create directories for the parent directories plus a directory with the same name as the filename instead of just directories for the parent directories when given the combined path and filename. The pathFilename may be just a filename without a path or a a file in the root directory; in these cases, no directory will be created.


replaceStrings

public static boolean replaceStrings(File file,
                                     String oldString,
                                     String newString)
                              throws FileNotFoundException,
                                     IOException
Replaces instances of oldString with newString in the inFile.

Creates a temporary file (inFile + ".tmp") in the process.

Returns:
Returns true if the file was updated.
Throws:
FileNotFoundException
IOException

replaceBytes

public static boolean replaceBytes(File file,
                                   byte[] oldBytes,
                                   byte[] newBytes)
                            throws FileNotFoundException,
                                   IOException
Replaces instances of oldBytes with newBytes in the inFile.

Creates a temporary file (inFile + ".tmp") in the process.

Returns:
Returns true if the file was updated.
Throws:
FileNotFoundException
IOException

replaceStrings

public static boolean replaceStrings(File inFile,
                                     File outFile,
                                     String oldString,
                                     String newString)
                              throws FileNotFoundException,
                                     IOException
Copies inFile to outFile with the replacement of occurrences of oldString with newString.

Any pre-existing outFile is overwritten.

Returns:
Returns true if outFile differs from inFile.
Throws:
FileNotFoundException
IOException

replaceBytes

public static boolean replaceBytes(File inFile,
                                   File outFile,
                                   byte[] oldBytes,
                                   byte[] newBytes)
                            throws FileNotFoundException,
                                   IOException
Copies inFile to outFile with the replacement of occurrences of oldBytes with newBytes.

Any pre-existing outFile is overwritten.

Returns:
Returns true if outFile differs from inFile.
Throws:
FileNotFoundException
IOException

loadTextFile

public static String loadTextFile(String filename)
                           throws IOException
Loads the text file content into a String.

Throws:
IOException

pareExtension

public static String pareExtension(String fileName)
Strips off the file name extension.


saveTextFile

public static void saveTextFile(String filename,
                                String text)
                         throws IOException
Saves text to a file.

Throws:
IOException

CroftSoft Javadoc

CroftSoft Core Javadoc (2008-09-28 20:58:02)