Class FileIO

java.lang.Object
  extended by FileIO
Direct Known Subclasses:
FileTable

public class FileIO
extends java.lang.Object

This class does various read/write file I/O and other file related functions.

 List of Methods
 FileIO() - Constructor, set some defaults.
 initFileIO() - Reset class variables for zip and random-access files
 promptFileName() - pop up prompt for file name
 mapPathFileSeparators() - map '/' or '\' to current file separ. for OS 
 readRandomAccessData() - read random access data from file.
 readRandomAccessLine() - read random access line from file with readLine.
 closeRandomAccessFile() - close random access file if was opened.
 fileExists() - test if file path exists and is readable
 readFileAsString() - read file from disk and return data as String
 writeStringToFile() - write string data to disk file.
 getFilesInDir() - get list of files in directory w/specified file ext.
 getSubDirsInDir() - get list of sub directories in directory.
 makeDirectory() - make a directory if it does not exist. 
 makePathSubDirs() - make a directory tree if it doesn't exist for path
 deleteFile() - Delete specific file specified with full path.
 copyFile() - binary copy of one file or URL to a local file
 copyDirectoryTree() - recursively copy directory tree.
 readStringFromURL() - read a String data from URL
 readBytesFromURL() - read binary data from URL 
 readFileZipEntries() - read zip file to a Vector of ZipEntry entry files
 getZipEntryName() - get the name of the ZipEntry item
 getZipEntryData() - get the data from a ZipEntry item
 getAllZipEntryNames() - get the names for all ZipEntry elements
 getAllZipEntryData() - get the data for all ZipEntry elements
 getAllZipEntries() - get the name and data for all zip entries
 readAllZipEntries() - get array of name and data for each zip entry
 writeZipFile() - zip the inputDir to a zipOutputFile.
 zipDir() - zip a directory into the zip output stream.
 getDirOfFile() - get the directory from a file path
 getTopLevelBackLink() - get the "../" Web backlink for current level.
 cvtRelDirToHyphenStr() - get the "../" Web backlink for current level.
 getLineSeparatorFromString() - get String line-term.: "\r", "\n" or "\r\n"
 getLineSeparatorFromFile() - get file line-terminator: "\r", "\n" or "\r\n"
 getDirPathByDirBrowser() - set the output path by directory browser
 testFile() - test if file exists and is readable.
 testDir() - test if file exists and is a readable directory.
 

This code is available at the HTMLtools project on SourceForge at http://htmltools.sourceforge.org/ under the "Common Public License Version 1.0" http://www.opensource.org/licenses/cpl1.0.php.

It was derived and refactored from the open source MAExplorer (http://maexplorer.sourceforge.org/), and Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules.

$Date: 2009/07/3 11:45:56 $ $Revision: 1.28 $
Copyright 2008, 2009 by Peter Lemkin E-Mail: lemkin@users.sourceforge.net http://lemkingroup.com/


Field Summary
 java.lang.String errMsgLog
          error message log it not empty
static java.lang.String lastErrMsgLog
          error message log it not empty.
 java.io.RandomAccessFile raf
          Random access file handle if used
 byte[] rafBuffer
          Random access file buffer if used
 java.lang.String rafFileName
          Random access filename if used
 long rafLastSeekPtr
          Last random access seek pointer
 java.util.zip.ZipFile zipFile
          ZipFile instance that contains the decoders and other things we need for unpacking the zip file.
 
Constructor Summary
FileIO()
          FileIO() - generic Constructor, set some defaults to 0.
 
Method Summary
 boolean closeRandomAccessFile()
          closeRandomAccessFile() - close random access file if was opened.
 boolean copyDirectoryTree(java.io.File srcPath, java.io.File dstPath)
          copyDirectoryTree() - recursively copy directory tree.
 boolean copyDirectoryTree(java.lang.String srcPath, java.lang.String dstPath)
          copyDirectoryTree() - recursively copy directory tree.
 boolean copyFile(java.lang.String srcName, java.lang.String dstName)
          copyFile() - binary copy of one file or URL to a local file
static java.lang.String cvtRelDirToHyphenStr(java.lang.String relDir)
          cvtRelDirToHyphenStr() - get the "../" Web backlink for current level.
 boolean deleteFile(java.lang.String fullFileName)
          deleteFile() - Delete specific file specified with full path.
 boolean fileExists(java.lang.String filePath)
          fileExists() - test if file path exists and is readable
 java.lang.String[][] getAllZipEntries(java.util.zip.ZipFile zf, java.util.Vector zipEntryList)
          getAllZipEntries() - get the name and data for all zip entries The returned array is of form
 java.lang.String[] getAllZipEntryData(java.util.Vector zipEntryList)
          getAllZipEntryData() - get the data for all ZipEntry elements
 java.lang.String[] getAllZipEntryNames(java.util.Vector zipEntryList)
          getAllZipEntryNames() - get the names for all ZipEntry elements
 java.lang.String getDirOfFile(java.lang.String inputDirPath)
          getDirOfFile() - get the directory from a file path
static java.lang.String getDirPathByDirBrowser(java.lang.String defaultDir, java.lang.String sPrompt)
          getDirPathByDirBrowser() - set the output path by directory browser
 java.lang.String[] getFilesInDir(java.lang.String dir, java.lang.String ext)
          getFilesInDir() - get list of files in directory with specified file extension
 java.lang.String getLineSeparatorFromFile(java.lang.String inputFile)
          getLineSeparatorFromFile() - get file line-terminator: "\r", "\n" or "\r\n".
 java.lang.String getLineSeparatorFromString(java.lang.String str)
          getLineSeparatorFromString() - get String line-term: "\r", "\n" or "\r\n".
 java.lang.String[] getSubDirsInDir(java.lang.String dir)
          getSubDirsInDir() - get list of sub directories in directory.
static java.lang.String getTopLevelBackLink(java.lang.String relDir)
          getTopLevelBackLink() - get the "../" Web backlink for current level.
 java.lang.String getZipEntryData(java.util.zip.ZipEntry zipEntry)
          getZipEntryData() - get the data from a ZipEntry item
 java.lang.String getZipEntryName(java.util.zip.ZipEntry zipEntry)
          getZipEntryName() - get the name of the ZipEntry item
 void initFileIO()
          initFileIO() - Reset class variables for zip and random-access files
 boolean makeDirectory(java.lang.String dirName, boolean allowMkdirsFlag)
          makeDirectory() - make a directory tree if it doesn't exist if allowMkdirsFlag using mkdirs().
 boolean makePathSubDirs(java.lang.String filePath)
          makePathSubDirs() - make a directory tree if it doesn't exist for path the file path.
static java.lang.String mapPathFileSeparators(java.lang.String str)
          mapPathFileSeparators() - map '/' or '\' to current file separators for OS
 java.lang.String promptFileName(java.lang.String promptDir, java.lang.String msg)
          promptFileName() - pop up prompt for file name
 java.lang.String[][] readAllZipEntries(java.lang.String zipFileName)
          readAllZipEntries() - get array of name and data for each zip entry The returned array is of form
 byte[] readBytesFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
          readBytesFromURL() - read binary data from URL
 java.lang.String readFileAsString(java.io.File f)
          readFileAsString() - Will read file from disk & returns as String
 java.lang.String readFileAsString(java.lang.String fileName)
          readFileAsString() - read file from disk and return data as String
 java.util.Vector readFileZipEntries(java.lang.String zipFileName)
          readFileZipEntries() - read zip file to a Vector of ZipEntry entry files but no directory items.
 java.lang.String readRandomAccessData(java.lang.String fileName, long startBytePtr, long endBytePtr)
          readRandomAccessData() - read random access data from file.
 java.lang.String readRandomAccessLine(java.lang.String fileName, long startBytePtr)
          readRandomAccessLine() - read random access line from file with readLine.
 java.lang.String readStringFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
          readStringFromURL() - read a String data from URL
 boolean testDir(java.lang.String dirPath)
          testDir() - test if file exists and is a readable directory.
 boolean testFile(java.lang.String filePath)
          testFile() - test if file exists and is readable.
 boolean writeStringToFile(java.lang.String fileName, java.lang.String data)
          writeStringToFile() - write string data to disk file.
 boolean writeZipFile(java.lang.String inputDirPath, java.lang.String zipOutputFile)
          writeZipFile() - zip the inputDir to a zipOutputFile.
 boolean zipDir(java.lang.String inputDirPath, java.lang.String dir2zip, java.util.zip.ZipOutputStream zos)
          zipDir() - zip a directory into the zip output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

errMsgLog

public java.lang.String errMsgLog
error message log it not empty


lastErrMsgLog

public static java.lang.String lastErrMsgLog
error message log it not empty. In case the function returning a FileTable instance is null, we can still get at the error msg.


zipFile

public java.util.zip.ZipFile zipFile
ZipFile instance that contains the decoders and other things we need for unpacking the zip file.


raf

public java.io.RandomAccessFile raf
Random access file handle if used


rafFileName

public java.lang.String rafFileName
Random access filename if used


rafBuffer

public byte[] rafBuffer
Random access file buffer if used


rafLastSeekPtr

public long rafLastSeekPtr
Last random access seek pointer

Constructor Detail

FileIO

public FileIO()
FileIO() - generic Constructor, set some defaults to 0.

Method Detail

initFileIO

public void initFileIO()
initFileIO() - Reset class variables for zip and random-access files


promptFileName

public java.lang.String promptFileName(java.lang.String promptDir,
                                       java.lang.String msg)
promptFileName() - pop up prompt for file name

Parameters:
promptDir - default dir
msg - message to display in file dialog
Returns:
full file path if successful else null
See Also:
Frame, FileDialog, FileDialog.setDirectory(java.lang.String), Component.setVisible(boolean), FileDialog.getFile(), FileDialog.getDirectory()

mapPathFileSeparators

public static java.lang.String mapPathFileSeparators(java.lang.String str)
mapPathFileSeparators() - map '/' or '\' to current file separators for OS

Parameters:
str - to map
Returns:
mapped string

readRandomAccessLine

public java.lang.String readRandomAccessLine(java.lang.String fileName,
                                             long startBytePtr)
readRandomAccessLine() - read random access line from file with readLine. Open the file if not previously opened. Read line until "\n", "\r", or "\r\n".

Parameters:
raf - RandomAccessFile to read
startBytePtr - to start reading
Returns:
string read from the file. If any errors, return null.
See Also:
closeRandomAccessFile(), RandomAccessFile

readRandomAccessData

public java.lang.String readRandomAccessData(java.lang.String fileName,
                                             long startBytePtr,
                                             long endBytePtr)
readRandomAccessData() - read random access data from file. Open the file if not previously opened. Read data from startBytePtr to endBytePtr.

Parameters:
raf - RandomAccessFile to read
startBytePtr - to start reading
endBytePtr - to stop reading.
Returns:
string read from the file. If any errors, return null.
See Also:
closeRandomAccessFile(), RandomAccessFile

closeRandomAccessFile

public boolean closeRandomAccessFile()
closeRandomAccessFile() - close random access file if was opened. It also clears class variables (raf, rafName, rafBuffer, rafLastSeekPtr).

Returns:
string read from the file. If any errors, return null.
See Also:
readRandomAccessLine(java.lang.String, long), RandomAccessFile

fileExists

public boolean fileExists(java.lang.String filePath)
fileExists() - test if file path exists and is readable

Parameters:
filePath - file name to test
Returns:
true if file exists and is readable
See Also:
File, readFileAsString(java.lang.String)

readFileAsString

public java.lang.String readFileAsString(java.lang.String fileName)
readFileAsString() - read file from disk and return data as String

Parameters:
fileName - file name to read data
Returns:
data as String, null if failed
See Also:
File, readFileAsString(java.lang.String)

readFileAsString

public java.lang.String readFileAsString(java.io.File f)
readFileAsString() - Will read file from disk & returns as String

Parameters:
f - File to read
Returns:
data read from the file, else null if failed.
See Also:
RandomAccessFile, RandomAccessFile#canRead, RandomAccessFile#exists, RandomAccessFile.readFully(byte[]), RandomAccessFile.close(), System.runFinalization(), UtilCM.mapCRLForCR2LF(java.lang.String)

writeStringToFile

public boolean writeStringToFile(java.lang.String fileName,
                                 java.lang.String data)
writeStringToFile() - write string data to disk file.

Parameters:
fileName - is name of the file to write
data - to write to the file
Returns:
true if successful false if failed
See Also:
File, File.canWrite(), FileWriter, OutputStreamWriter.write(int), OutputStreamWriter.close()

getFilesInDir

public java.lang.String[] getFilesInDir(java.lang.String dir,
                                        java.lang.String ext)
getFilesInDir() - get list of files in directory with specified file extension

Parameters:
dir - directory to read
ext - is the file extensions (e.g. ".txt") to match if not null, else accept all files
Returns:
list of files, null if fail or there are no files

getSubDirsInDir

public java.lang.String[] getSubDirsInDir(java.lang.String dir)
getSubDirsInDir() - get list of sub directories in directory. This ignores files which are not sub directories.

Parameters:
dir - directory to read
Returns:
list of files, null if fail or there are no files

makeDirectory

public boolean makeDirectory(java.lang.String dirName,
                             boolean allowMkdirsFlag)
makeDirectory() - make a directory tree if it doesn't exist if allowMkdirsFlag using mkdirs(). Otherwise, just report whether the directory tree exists.

Parameters:
dirName - to check and possibly create. It may have multiple subdirectories
allowMkdirsFlag - to allow mkdirs() if directories not found
Returns:
true if succeed in either finding the directory or in creating it, false if I/O error or a directory was NOT found and allowMkdirsFlag was NOT set.

makePathSubDirs

public boolean makePathSubDirs(java.lang.String filePath)
makePathSubDirs() - make a directory tree if it doesn't exist for path the file path. Strip off the file at the end by finding the last fileSeparator, then do makeDirectory().

Parameters:
filePath - to create the sub directories along the path
Returns:
true if succeed

deleteFile

public boolean deleteFile(java.lang.String fullFileName)
deleteFile() - Delete specific file specified with full path.

Parameters:
fullFileName - of file to delete

copyFile

public boolean copyFile(java.lang.String srcName,
                        java.lang.String dstName)
copyFile() - binary copy of one file or URL to a local file

Parameters:
srcName - is either a full path local file name or a http:// prefixed URL string of the source file.
dstName - is the full path of the local destination file name
Returns:
true if succeed.

copyDirectoryTree

public boolean copyDirectoryTree(java.lang.String srcPath,
                                 java.lang.String dstPath)
copyDirectoryTree() - recursively copy directory tree. It does not preserve the files creation dates. Derived from code on http://www.roseindia.net/java/example/java/io/copyDirectory.shtml

Parameters:
srcPath -
dstPath -
Returns:
true if succeed, false if error and message in errMsgLog

copyDirectoryTree

public boolean copyDirectoryTree(java.io.File srcPath,
                                 java.io.File dstPath)
copyDirectoryTree() - recursively copy directory tree. It does not preserve the files creation dates. Derived from code on http://www.roseindia.net/java/example/java/io/CopyDirectory.shtml

Parameters:
srcPath -
dstPath -
Returns:
true if succeed, false if error and message in errMsgLog

readStringFromURL

public java.lang.String readStringFromURL(java.lang.String srcName,
                                          java.lang.String optUpdateMsg)
readStringFromURL() - read a String data from URL

Parameters:
srcName - is either a full path local file name or a http:// prefixed URL string of the source file.
optUpdateMsg - (opt) will display message in logMsg() and increasing ... in logMsg(). One '.' for every 10K bytes read. This only is used when reading a URL. Set to null if not used.
Returns:
a String if succeed, else null.

readBytesFromURL

public byte[] readBytesFromURL(java.lang.String srcName,
                               java.lang.String optUpdateMsg)
readBytesFromURL() - read binary data from URL

Parameters:
srcName - is either a full path local file name or a http:// prefixed URL string of the source file.
optUpdateMsg - (opt) will display message in logMsg() and increasing ... in logMsg(). One '.' for every 10K bytes read. This only is used when reading a URL. Set to null if not used.
Returns:
a byte[] if succeed, else null.

readFileZipEntries

public java.util.Vector readFileZipEntries(java.lang.String zipFileName)
readFileZipEntries() - read zip file to a Vector of ZipEntry entry files but no directory items.

Parameters:
zipFileName -
Returns:
a Vector of zip file ZipEntry file entries, else null if failed.

getZipEntryName

public java.lang.String getZipEntryName(java.util.zip.ZipEntry zipEntry)
getZipEntryName() - get the name of the ZipEntry item

Parameters:
zipEntry - data
Returns:
zip entry name, else null if failed.

getZipEntryData

public java.lang.String getZipEntryData(java.util.zip.ZipEntry zipEntry)
getZipEntryData() - get the data from a ZipEntry item

Parameters:
zipEntry - data
Returns:
data of ZipEntry entry, else null if failed.

getAllZipEntryNames

public java.lang.String[] getAllZipEntryNames(java.util.Vector zipEntryList)
getAllZipEntryNames() - get the names for all ZipEntry elements

Parameters:
zipEntryList - (ZipEntry) to analyze
Returns:
array of zip file names, else null if failed.

getAllZipEntryData

public java.lang.String[] getAllZipEntryData(java.util.Vector zipEntryList)
getAllZipEntryData() - get the data for all ZipEntry elements

Parameters:
zipEntryList - (ZipEntry) to analyze
Returns:
array of zip file names, else null if failed.

getAllZipEntries

public java.lang.String[][] getAllZipEntries(java.util.zip.ZipFile zf,
                                             java.util.Vector zipEntryList)
getAllZipEntries() - get the name and data for all zip entries The returned array is of form
    dataList[0:nEntries-1][0:1]
 where:
    dataList[*][0]   = zip file names
    dataList[*][1]   = unzipped data
 

Parameters:
zipEntryList - (ZipEntry) to analyze
Returns:
array of unzipped data , else null if failed.

readAllZipEntries

public java.lang.String[][] readAllZipEntries(java.lang.String zipFileName)
readAllZipEntries() - get array of name and data for each zip entry The returned array is of form
    dataList[0:nEntries-1][0:1]
 where:
    dataList[*][0]   = zip file names
    dataList[*][1]   = unzipped data
 

Parameters:
zipEntryName - to read
Returns:
array of unzipped data, else null if failed.

writeZipFile

public boolean writeZipFile(java.lang.String inputDirPath,
                            java.lang.String zipOutputFile)
writeZipFile() - zip the inputDir to a zipOutputFile. [TODO] debug subfolder path issue where it saves the zip directory in the complete path rather than the directory tree at the end of the path. Can't use writeZipFile() until fix this.

Code derived from http://www.devx.com/tips/Tip/14049 and discussion in the example (pg 1922-1924) in Chan, Lee, Kramer "The Java Class Libraries, 2nd ed., Vol 1, Addison-Wesley, 1998.

Parameters:
inputDirPath - - path of directory to zip. Use the lowest directory in the tree as the name so it unzips it into that directory.
zipOutputFile - - zip output file
Returns:
true if succeed, error in errMsgLog

zipDir

public boolean zipDir(java.lang.String inputDirPath,
                      java.lang.String dir2zip,
                      java.util.zip.ZipOutputStream zos)
zipDir() - zip a directory into the zip output stream.

Code derived from http://www.devx.com/tips/Tip/14049 and discussion in the example (pg 1922-1924) in Chan, Lee, Kramer "The Java Class Libraries, 2nd ed., Vol 1, Addison-Wesley, 1998.

Parameters:
inputDirPath - - path of directory to zip.
dir2zip - - bottom directory name to zip
zos - - ZipOutputStream
Returns:
true if succeed, error in errMsgLog

getDirOfFile

public java.lang.String getDirOfFile(java.lang.String inputDirPath)
getDirOfFile() - get the directory from a file path

Parameters:
inputDirPath - - file path
Returns:
path of file

getTopLevelBackLink

public static java.lang.String getTopLevelBackLink(java.lang.String relDir)
getTopLevelBackLink() - get the "../" Web backlink for current level.

Parameters:
relDir - - the relative relDir from the top level of Web site. Note the relDir must end in a directory not in a specific file.
Returns:
String with back link list of "../../" etc., else ""

cvtRelDirToHyphenStr

public static java.lang.String cvtRelDirToHyphenStr(java.lang.String relDir)
cvtRelDirToHyphenStr() - get the "../" Web backlink for current level.

Parameters:
relDir - - the relative relDir from the top level of Web site. Note the relDir must end in a directory not in a specific file.
Returns:
String with back link list of "../../" etc., else ""

getLineSeparatorFromString

public java.lang.String getLineSeparatorFromString(java.lang.String str)
getLineSeparatorFromString() - get String line-term: "\r", "\n" or "\r\n".

Parameters:
str - used to determine the line-terminator.
Returns:
"\r", "\n" or "\r\n" if succeed, null if not found or error.

getLineSeparatorFromFile

public java.lang.String getLineSeparatorFromFile(java.lang.String inputFile)
getLineSeparatorFromFile() - get file line-terminator: "\r", "\n" or "\r\n". Read enough of the file to determine the line terminator.

Parameters:
inputFile - to read to determine the line-terminator.
Returns:
"\r", "\n" or "\r\n" if succeed, null if not found or error.

getDirPathByDirBrowser

public static java.lang.String getDirPathByDirBrowser(java.lang.String defaultDir,
                                                      java.lang.String sPrompt)
getDirPathByDirBrowser() - set the output path by directory browser

Parameters:
defaultDir - if not null
sPrompt - to use if not null
Returns:
directory if not null.

testFile

public boolean testFile(java.lang.String filePath)
testFile() - test if file exists and is readable.

Parameters:
filePath - to test
Returns:
true if file exists and is readable

testDir

public boolean testDir(java.lang.String dirPath)
testDir() - test if file exists and is a readable directory.

Parameters:
dirPath - to test
Returns:
true if directory exists and is readable