com.bluestream.sys.util
Class FileUtil

java.lang.Object
  extended bycom.bluestream.sys.util.FileUtil

public class FileUtil
extends Object

FileUtil: The primary purpose here is to maintain a process wide concept of the Current Working Directory (or CWD). NOTE: This class currently does not make use of the System object as there are known bugs in that package. At some point in the future, when System is known to provide reliable results, this modules implementation could be changed to rely on System.


Constructor Summary
FileUtil()
           
 
Method Summary
static void allowFileRead(String filename)
           
static void appendFile(String fileName, String output)
          append to a file
static void buildTree(String path, boolean fContainsFN)
          Build a tree on the file system
static boolean compareTextFiles(String fn1, String fn2, int flags, FlexStringBuffer fsbErrs)
           
static void copyFile(File srcFile, File destFile)
          Copy the contents of srcFile to destFile.
static void copyFile(String fileSrc, String fileDest)
           
static File createFileInSpecifiedDir(File workingDir, String createdDirName, String createdFileName)
          This method creates a directory called 'createdDirName' inside the 'workingDir' directory, and then creates a file called 'createdFileName' located inside the newly created directory.
static boolean deleteFile(File fileToDelete)
          Checks for the existence of the passed in File, and deletes the file if it does exist.
static String getCWD()
          return the CWD string
static String getExt(String fullName)
          get the extension of the file name (after the dot)not including dot.
static String getName(String fullName)
          Strip the file name from the path
static String getPath(String fullStr)
          Strip the file name from the path
static String getPrimary(String name)
          Get the primary part of a file name.
static String getVolume(String fullName)
          Get the volume from the fullname.
static String readAsEncodedXml(File file)
           
static String readAsEncodedXml(String fileName)
           
static byte[] readBinary(File file)
          Reads a file into a byte[] without changing the encoding.
static void readBinary(File file, byte[] b)
          Read binary file.
static String readFile(File file)
           
static String readFile(String fileName)
          read a file into a string.
static String readFileNoComments(File file)
          Read a file and strip out single line comments that begin with double slash in the first position.
static String readRecent(File file, int recentLineCount)
          Reads last recentLineCount number of lines from the file into a string.
static void removeDir(File dir)
          recursively removes a directory
static void setCWD(String cwd)
          assign a new directory to be the CWD
static void setLinuxFilePermission(String permission)
          Sets a file permission for Linux, Unix and Mac.
static void stripCR(File f)
          strips all CR characters from the TEXT file specified.
static String stripVolume(String fullName)
          Strip the volume from the fullname.
static void writeAsEncodedXml(String filename, String content)
           
static void writeBinary(File file, byte[] b)
          Write binary file.
static void writeFile(String fileName, String output)
          Simple - write string to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

setCWD

public static void setCWD(String cwd)
assign a new directory to be the CWD


getCWD

public static String getCWD()
return the CWD string


getPrimary

public static String getPrimary(String name)
Get the primary part of a file name. The primary is the name before the dot, or if no name, then the passed in name. Note: If you pass in a fullName, then you get back a fullName, if you pass in a shortName, then you get back a shortName.


getPath

public static String getPath(String fullStr)
Strip the file name from the path


getName

public static String getName(String fullName)
Strip the file name from the path


getExt

public static String getExt(String fullName)
get the extension of the file name (after the dot)not including dot. Pass in either fullName or short name.


getVolume

public static String getVolume(String fullName)
Get the volume from the fullname. If no volume is found return empty string.

Returns:
volume. For example 'c:'

stripVolume

public static String stripVolume(String fullName)
Strip the volume from the fullname. If no volume is no problem.

Returns:
fullname minus volume. For example '/a/b'

readFile

public static String readFile(String fileName)
read a file into a string.


readFile

public static String readFile(File file)

readFileNoComments

public static String readFileNoComments(File file)
                                 throws IOException
Read a file and strip out single line comments that begin with double slash in the first position. Note: the actual line feed will not be stripped out.

Throws:
IOException

readRecent

public static String readRecent(File file,
                                int recentLineCount)
                         throws IOException
Reads last recentLineCount number of lines from the file into a string.

Parameters:
file - - file to be read.
recentLineCount - - number of lines to be read.
Returns:
string containing recentLineCount number of file.
Throws:
IOException

writeFile

public static void writeFile(String fileName,
                             String output)
Simple - write string to a file.


appendFile

public static void appendFile(String fileName,
                              String output)
append to a file


removeDir

public static void removeDir(File dir)
recursively removes a directory


stripCR

public static void stripCR(File f)
                    throws IOException
strips all CR characters from the TEXT file specified. This can be used to clean up text files prior to their use on Linux/Solaris systems.

Throws:
IOException

copyFile

public static void copyFile(String fileSrc,
                            String fileDest)
                     throws IOException
Throws:
IOException

copyFile

public static void copyFile(File srcFile,
                            File destFile)
                     throws IOException
Copy the contents of srcFile to destFile.

Throws:
IOException

readBinary

public static void readBinary(File file,
                              byte[] b)
                       throws IOException,
                              FileNotFoundException
Read binary file.

Throws:
IOException
FileNotFoundException

readBinary

public static byte[] readBinary(File file)
Reads a file into a byte[] without changing the encoding. This is useful for reading binary data that should not be altered during the read.

Parameters:
file - The file to read.
Returns:
byte[] The exact content of the file.
Throws:
BaseException - Thrown if an error occurs while reading the file.

writeBinary

public static void writeBinary(File file,
                               byte[] b)
                        throws IOException,
                               FileNotFoundException
Write binary file.

Throws:
IOException
FileNotFoundException

writeAsEncodedXml

public static void writeAsEncodedXml(String filename,
                                     String content)
                              throws Exception
Throws:
Exception

readAsEncodedXml

public static String readAsEncodedXml(String fileName)

readAsEncodedXml

public static String readAsEncodedXml(File file)

compareTextFiles

public static boolean compareTextFiles(String fn1,
                                       String fn2,
                                       int flags,
                                       FlexStringBuffer fsbErrs)
Parameters:
fn1 - first reader (Candidate)
fn2 - second reader (Reference)
flags - CompareUtil.kContinueOnErr, or 0 for no flags.
Returns:
true for identical, false for different.

allowFileRead

public static void allowFileRead(String filename)

setLinuxFilePermission

public static void setLinuxFilePermission(String permission)
Sets a file permission for Linux, Unix and Mac.

Parameters:
permission - a string representing the permission that would be entered on a commandline, eg "chmod 777 runServer.sh"

buildTree

public static void buildTree(String path,
                             boolean fContainsFN)
Build a tree on the file system

Parameters:
path - full path - usually without file name eg: c:/a/b. Must have volume name.
fContainsFN - Whether a filename is contained in path parameter.

createFileInSpecifiedDir

public static File createFileInSpecifiedDir(File workingDir,
                                            String createdDirName,
                                            String createdFileName)
This method creates a directory called 'createdDirName' inside the 'workingDir' directory, and then creates a file called 'createdFileName' located inside the newly created directory.

Parameters:
workingDir - File object for the current working directory.
createdDirName - String name of the containing directory to be created in the current working directory.
createdFileName - String name of the new file to be created.
Returns:
File new file that has been created in the directory named 'createdDirName', that was created inside 'workingDir'.

deleteFile

public static boolean deleteFile(File fileToDelete)
Checks for the existence of the passed in File, and deletes the file if it does exist. If the passed in File parameter is null, or does not exist, no error occurs and a boolean 'false' is returned.

Parameters:
fileToDelete - File to be deleted.
Returns:
boolean 'true' is returned if the file deletion was successful.


Copyright 2006 Bluestream Database Software Corp. All Rights Reserved.