com.amx.duet.tools.io
Class FileUtil

java.lang.Object
  extended bycom.amx.duet.tools.io.FileUtil

public class FileUtil
extends java.lang.Object

A utility class that performs file operations similar to NetLinx file operations.


Field Summary
static int FILE_ALREADY_CLOSE
           
static int FILE_BUFFER_TOO_SMALL
           
static int FILE_DIRECTORY_EXISTS
           
static int FILE_DIRECTORY_NOT_LOADED
           
static int FILE_DISK_ERROR
           
static int FILE_DISK_FULL
           
static int FILE_END_OF_FILE
           
static int FILE_INVALID_DIRECTORY
           
static int FILE_INVALID_FILENAME
           
static int FILE_INVALID_HANDLE
           
static int FILE_INVALID_IOFLAG
           
static int FILE_INVALID_PARAMETER
           
static int FILE_NAME_EXISTS
           
static int FILE_OK
           
static int IOFLAG_APPEND
           
static int IOFLAG_NEW
           
static int IOFLAG_READ
           
 
Constructor Summary
FileUtil()
           
 
Method Summary
static boolean deleteDir(java.io.File dir)
          Deletes all files and subdirectories under the specified directory.
static long FileClose(long lFileHandle)
          Closes a file opened with FileOpen().
static long FileCopy(java.lang.String sSrcFilePath, java.lang.String sDstFilePath)
          Copies the specified file from the source location to the specified destination or name.
static long FileCreateDir(java.lang.String sDirPath)
          Create the specified directory path.
static long FileDelete(java.lang.String sFilePath)
          Deletes the specified file(s).
static long FileDir(java.lang.String sDirPath, ObjectRef rStrBuffer, ObjectRef rEntry)
          Returns a list of files located at the specified path.
static long FileGetDir(ObjectRef rStrDirPath)
          Returns the current working directory.
static long FileOpen(java.lang.String sFilePath, long lIOFlag)
          Opens a file for reading or writing.
static long FileRead(long lFileHandle, ObjectRef rStrBuffer, long lBufLen)
          Reads a block of data from the specified file.
static long FileReadLine(long lFileHandle, ObjectRef rStrBuffer, long lBufLen)
          Reads a line of data from the specified file.
static long FileRemoveDir(java.lang.String sDirPath)
          Removes the specified directory path and all subdirectories below it.
static long FileRename(java.lang.String sFilePath, java.lang.String sNewFileName)
          Renames the specified file.
static long FileSeek(long lFileHandle, long lPos)
          Sets the file pointer to the specified position.
static long FileSize(long lFileHandle)
          Retrieves the size of a file on disk.
static long FileSize(java.lang.String sFilePath)
          Retrieves the size of a file on disk.
static long FileWrite(long lFileHandle, java.lang.String sBuffer, long lBufLen)
          Writes a block of data to the specified file.
static long FileWriteLine(long lFileHandle, java.lang.String sBuffer, long lLineLen)
          Writes a line of data to the specified file.
static java.lang.String getUserDirectory(java.lang.String path)
          Determines if the path passed in has the user directory prepended to it.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_OK

public static final int FILE_OK
See Also:
Constant Field Values

FILE_INVALID_HANDLE

public static final int FILE_INVALID_HANDLE
See Also:
Constant Field Values

FILE_INVALID_FILENAME

public static final int FILE_INVALID_FILENAME
See Also:
Constant Field Values

FILE_INVALID_IOFLAG

public static final int FILE_INVALID_IOFLAG
See Also:
Constant Field Values

FILE_INVALID_DIRECTORY

public static final int FILE_INVALID_DIRECTORY
See Also:
Constant Field Values

FILE_DISK_ERROR

public static final int FILE_DISK_ERROR
See Also:
Constant Field Values

FILE_INVALID_PARAMETER

public static final int FILE_INVALID_PARAMETER
See Also:
Constant Field Values

FILE_ALREADY_CLOSE

public static final int FILE_ALREADY_CLOSE
See Also:
Constant Field Values

FILE_NAME_EXISTS

public static final int FILE_NAME_EXISTS
See Also:
Constant Field Values

FILE_END_OF_FILE

public static final int FILE_END_OF_FILE
See Also:
Constant Field Values

FILE_BUFFER_TOO_SMALL

public static final int FILE_BUFFER_TOO_SMALL
See Also:
Constant Field Values

FILE_DISK_FULL

public static final int FILE_DISK_FULL
See Also:
Constant Field Values

FILE_DIRECTORY_NOT_LOADED

public static final int FILE_DIRECTORY_NOT_LOADED
See Also:
Constant Field Values

FILE_DIRECTORY_EXISTS

public static final int FILE_DIRECTORY_EXISTS
See Also:
Constant Field Values

IOFLAG_READ

public static final int IOFLAG_READ
See Also:
Constant Field Values

IOFLAG_NEW

public static final int IOFLAG_NEW
See Also:
Constant Field Values

IOFLAG_APPEND

public static final int IOFLAG_APPEND
See Also:
Constant Field Values
Constructor Detail

FileUtil

public FileUtil()
Method Detail

FileClose

public static final long FileClose(long lFileHandle)
Closes a file opened with FileOpen(). This function should be called when all reading or writing to the file is completed.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
Returns:
  • 0 = operation was successful
  • -1 = invalid file handle
  • -7 = file already closed
  • -5 = disk I/O error

FileCopy

public static final long FileCopy(java.lang.String sSrcFilePath,
                                  java.lang.String sDstFilePath)
Copies the specified file from the source location to the specified destination or name. If either path name fails to specify a directory, the current working directory is assumed.

Parameters:
sSrcFilePath - path name of the file to copy (source).
sDstFilePath - path name of the copied file (destination).
Returns:
  • 0 = operation was successful
  • -2 = invalid file name
  • -11 = disk full
  • -5 = disk I/O error

FileCreateDir

public static final long FileCreateDir(java.lang.String sDirPath)
Create the specified directory path. This method will also create the number of subdirectories needed to complete the directory path.

Parameters:
sDirPath - string containing the directory path to create.
Returns:
  • 0 = operation was successful
  • -4 = invalid directory path
  • -5 = disk I/O error
  • -13 = directory name exists

FileDelete

public static final long FileDelete(java.lang.String sFilePath)
Deletes the specified file(s).

Parameters:
sFilePath - path name of the file to delete. Wildcard characters (* and ?) are permitted in the path name.
Returns:
  • 0 = operation was successful
  • -2 = invalid file path or name
  • -5 = disk I/O error

FileDir

public static final long FileDir(java.lang.String sDirPath,
                                 ObjectRef rStrBuffer,
                                 ObjectRef rEntry)
Returns a list of files located at the specified path.

Each directory entry will have a character pair appended to the end.

Parameters:
sDirPath - String containing the path to the requested directory.
rStrBuffer - Must be of type String. buffer to hold the directory list.

Create rStrBuffer with a valueOf(String) Example:
ObjectRef rStrBuffer = new ObjectRef("") or rStrBuffer.valueOf("")

rEntry - Must be of type String. requested directory entry..

Create rEntry with a valueOf(String) Example:
ObjectRef rEntry = new ObjectRef("") or rEntry.valueOf("")

Returns:
This method returns the number of remaining files in the directory, or:
  • -4 = invalid directory path
  • -10 = buffer too small
  • -6 = invalid parameter (i.e. Entry points beyond the end of the directory)
  • -12 = directory not loaded
  • -5 = Disk I/O error

FileGetDir

public static final long FileGetDir(ObjectRef rStrDirPath)
Returns the current working directory.

Parameters:
rStrDirPath - Must be of type String. Buffer to receive the current working directory.

Create rStrDirPath with a valueOf(String) Example:
ObjectRef rStrDirPath = new ObjectRef("") or rStrDirPath.valueOf("")

Returns:
  • 0 = operation was successful
  • -10 = size of DirPath buffer insufficient to hold directory path name

FileOpen

public static final long FileOpen(java.lang.String sFilePath,
                                  long lIOFlag)
Opens a file for reading or writing.

If the file is opened successfully, it must be closed (after all reading or writing is completed) by calling FileClose(). If files are not closed, subsequent file open operations may fail due to the limited number of file handles available.

Parameters:
sFilePath - String containing the path to the file to be opened.
Returns:
If the open operation is successful, this function returns a non-zero integer value representing the handle to the file. This handle must be used in subsequent read, write and close operations.
  • 0 = handle to file (open was successful)
  • -2 = invalid file path or name
  • -5 = disk I/O error
  • -3 = invalid value supplied for IOFlag

FileRead

public static final long FileRead(long lFileHandle,
                                  ObjectRef rStrBuffer,
                                  long lBufLen)
Reads a block of data from the specified file. This function reads (from the current location of the file pointer) the number of bytes specified by lBufLen or fewer bytes if the end of file is reached. The bytes are read from the file identified by lFileHandle and are stored in rStrBuffer. The file pointer will automatically be advanced the correct number of bytes so the next read operation continues where the last operation left off.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
rStrBuffer - Must be of type String. Buffer to hold the data to be read.

Create rStrBuffer with a valueOf(String) Example:
ObjectRef rStrBuffer = new ObjectRef("") or rStrBuffer.valueOf("")

lBufLen - maximum number of bytes to read.
Returns:
  • 0 = the number of bytes actually read
  • -1 = invalid file handle
  • -9 = end-of-file reached
  • -5 = disk I/O error
  • -6 = invalid parameter

FileReadLine

public static final long FileReadLine(long lFileHandle,
                                      ObjectRef rStrBuffer,
                                      long lBufLen)
Reads a line of data from the specified file.

This function reads from the current location of the file pointer up to the next carriage return or to the end-of-file (EOF), whichever comes first. A complete line will not be read if the buffer length is exceeded before a carriage return (or EOF) is encountered. The bytes are read from the file identified by lFileHandle and are stored in rStrBuffer. The or pair will not be stored in rStrBuffer. If a complete line is read, the file pointer is advanced to the next character in the file after the or pair or to the EOF if the last line was read.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
rStrBuffer - Must be of type String. Buffer to hold the data to be read.

Create rStrBuffer with a valueOf(String) Example:
ObjectRef rStrBuffer = new ObjectRef("") or rStrBuffer.valueOf("")

lBufLen - maximum number of bytes to read.
Returns:
  • 0 = the number of bytes actually read
  • -1 = invalid file handle
  • -9 = EOF (end-of-file) reached
  • -5 = disk I/O error
  • -6 = invalid parameter (buffer length must be greater than zero)

FileRemoveDir

public static final long FileRemoveDir(java.lang.String sDirPath)
Removes the specified directory path and all subdirectories below it. This function should be used with extreme caution.

This function will remove the specified directory and all subdirectories below that directory. It will also delete all files contained in any of these directories.

Parameters:
sDirPath - string containing the directory path to remove.
Returns:
  • 0 = operation was successful
  • -4 = invalid directory path
  • -5 = disk I/O error

FileRename

public static final long FileRename(java.lang.String sFilePath,
                                    java.lang.String sNewFileName)
Renames the specified file.

Parameters:
sFilePath - path name of the file to rename.
sNewFileName - new file name. This name must not contain a directory path.
Returns:
  • 0 = operation was successful
  • -2 = invalid file name
  • -8 = file name exists
  • -5 = disk I/O error

FileSize

public static final long FileSize(java.lang.String sFilePath)
Retrieves the size of a file on disk.

Parameters:
sFilePath - path name of the file to get the size of.
Returns:
the length of data contained in the file or:
  • 0 = Operation was successful and the file contains no data.
  • -2 = invalid file name
  • -5 = disk I/O error

FileSize

public static final long FileSize(long lFileHandle)
Retrieves the size of a file on disk.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
Returns:
the length of data contained in the file or:
  • 0 = Operation was successful and the file contains no data.
  • -1 = invalid file handle

FileSeek

public static final long FileSeek(long lFileHandle,
                                  long lPos)
Sets the file pointer to the specified position.

After FileSeek() is successfully called, subsequent read or write operations begin at the byte number specified by lPos.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
lPos - The byte position to set the file pointer (0 = beginning of file, -1 = end of file)
Returns:
  • 0 = Operation was successful and the result is the current file pointer value
  • -1 = invalid file handle
  • -6 = Invalid parameter (pos points beyond the end-of-file (position is set to the end-of-file))
  • -5 = disk I/O error

FileWrite

public static final long FileWrite(long lFileHandle,
                                   java.lang.String sBuffer,
                                   long lBufLen)
Writes a block of data to the specified file.

The data will overwrite or append to the current contents of the file depending on the current position of the file pointer.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
sBuffer - buffer containing the data to write.
lBufLen - number of bytes to write.
Returns:
  • 0 = the number of bytes actually written
  • -1 = invalid file handle
  • -11 = disk full
  • -5 = disk I/O error
  • -6 = invalid parameter (buffer length must be greater than zero)

FileWriteLine

public static final long FileWriteLine(long lFileHandle,
                                       java.lang.String sBuffer,
                                       long lLineLen)
Writes a line of data to the specified file.

A character string is automatically appended to the end of the line.

Parameters:
lFileHandle - handle to the file returned by FileOpen().
sBuffer - buffer containing the data to write.
lLineLen - number of bytes to write.
Returns:
  • 0 = the number of bytes actually written
  • -1 = invalid file handle
  • -11 = disk full
  • -5 = disk I/O error
  • -6 = invalid parameter (LineLen must be greater than zero)

deleteDir

public static final boolean deleteDir(java.io.File dir)
Deletes all files and subdirectories under the specified directory. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.

Parameters:
dir -
Returns:

getUserDirectory

public static final java.lang.String getUserDirectory(java.lang.String path)
Determines if the path passed in has the user directory prepended to it. If it does not, then this method will automatically prepend the user directory to the selected path in order to avoid security exceptions. If the path passed in does not contain any data, then the default user directory will be returned.

Parameters:
path - String to check for user directory (can be null or empty)
Returns:
Default user directory prepended String


Copyright © 2008 AMX LLC. All Rights Reserved.