|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object FileIO
public class FileIO
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 |
---|
public java.lang.String errMsgLog
public static java.lang.String lastErrMsgLog
public java.util.zip.ZipFile zipFile
public java.io.RandomAccessFile raf
public java.lang.String rafFileName
public byte[] rafBuffer
public long rafLastSeekPtr
Constructor Detail |
---|
public FileIO()
Method Detail |
---|
public void initFileIO()
public java.lang.String promptFileName(java.lang.String promptDir, java.lang.String msg)
promptDir
- default dirmsg
- message to display in file dialog
Frame
,
FileDialog
,
FileDialog.setDirectory(java.lang.String)
,
Component.setVisible(boolean)
,
FileDialog.getFile()
,
FileDialog.getDirectory()
public static java.lang.String mapPathFileSeparators(java.lang.String str)
str
- to map
public java.lang.String readRandomAccessLine(java.lang.String fileName, long startBytePtr)
raf
- RandomAccessFile to readstartBytePtr
- to start reading
closeRandomAccessFile()
,
RandomAccessFile
public java.lang.String readRandomAccessData(java.lang.String fileName, long startBytePtr, long endBytePtr)
raf
- RandomAccessFile to readstartBytePtr
- to start readingendBytePtr
- to stop reading.
closeRandomAccessFile()
,
RandomAccessFile
public boolean closeRandomAccessFile()
readRandomAccessLine(java.lang.String, long)
,
RandomAccessFile
public boolean fileExists(java.lang.String filePath)
filePath
- file name to test
File
,
readFileAsString(java.lang.String)
public java.lang.String readFileAsString(java.lang.String fileName)
fileName
- file name to read data
File
,
readFileAsString(java.lang.String)
public java.lang.String readFileAsString(java.io.File f)
f
- File to read
RandomAccessFile
,
RandomAccessFile#canRead
,
RandomAccessFile#exists
,
RandomAccessFile.readFully(byte[])
,
RandomAccessFile.close()
,
System.runFinalization()
,
UtilCM.mapCRLForCR2LF(java.lang.String)
public boolean writeStringToFile(java.lang.String fileName, java.lang.String data)
fileName
- is name of the file to writedata
- to write to the file
File
,
File.canWrite()
,
FileWriter
,
OutputStreamWriter.write(int)
,
OutputStreamWriter.close()
public java.lang.String[] getFilesInDir(java.lang.String dir, java.lang.String ext)
dir
- directory to readext
- is the file extensions (e.g. ".txt") to match if not
null, else accept all files
public java.lang.String[] getSubDirsInDir(java.lang.String dir)
dir
- directory to read
public boolean makeDirectory(java.lang.String dirName, boolean allowMkdirsFlag)
dirName
- to check and possibly create. It may have multiple
subdirectoriesallowMkdirsFlag
- to allow mkdirs() if directories not found
public boolean makePathSubDirs(java.lang.String filePath)
filePath
- to create the sub directories along the path
public boolean deleteFile(java.lang.String fullFileName)
fullFileName
- of file to deletepublic boolean copyFile(java.lang.String srcName, java.lang.String dstName)
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
public boolean copyDirectoryTree(java.lang.String srcPath, java.lang.String dstPath)
srcPath
- dstPath
-
public boolean copyDirectoryTree(java.io.File srcPath, java.io.File dstPath)
srcPath
- dstPath
-
public java.lang.String readStringFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
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.
public byte[] readBytesFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
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.
public java.util.Vector readFileZipEntries(java.lang.String zipFileName)
zipFileName
-
public java.lang.String getZipEntryName(java.util.zip.ZipEntry zipEntry)
zipEntry
- data
public java.lang.String getZipEntryData(java.util.zip.ZipEntry zipEntry)
zipEntry
- data
public java.lang.String[] getAllZipEntryNames(java.util.Vector zipEntryList)
zipEntryList
- (ZipEntry) to analyze
public java.lang.String[] getAllZipEntryData(java.util.Vector zipEntryList)
zipEntryList
- (ZipEntry) to analyze
public java.lang.String[][] getAllZipEntries(java.util.zip.ZipFile zf, java.util.Vector zipEntryList)
dataList[0:nEntries-1][0:1] where: dataList[*][0] = zip file names dataList[*][1] = unzipped data
zipEntryList
- (ZipEntry) to analyze
public java.lang.String[][] readAllZipEntries(java.lang.String zipFileName)
dataList[0:nEntries-1][0:1] where: dataList[*][0] = zip file names dataList[*][1] = unzipped data
zipEntryName
- to read
public boolean writeZipFile(java.lang.String inputDirPath, java.lang.String zipOutputFile)
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.
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
public boolean zipDir(java.lang.String inputDirPath, java.lang.String dir2zip, java.util.zip.ZipOutputStream zos)
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.
inputDirPath
- - path of directory to zip.dir2zip
- - bottom directory name to zipzos
- - ZipOutputStream
public java.lang.String getDirOfFile(java.lang.String inputDirPath)
inputDirPath
- - file path
public static java.lang.String getTopLevelBackLink(java.lang.String relDir)
relDir
- - the relative relDir from the top level of Web site.
Note the relDir must end in a directory not in a
specific file.
public static java.lang.String cvtRelDirToHyphenStr(java.lang.String relDir)
relDir
- - the relative relDir from the top level of Web site.
Note the relDir must end in a directory not in a
specific file.
public java.lang.String getLineSeparatorFromString(java.lang.String str)
str
- used to determine the line-terminator.
public java.lang.String getLineSeparatorFromFile(java.lang.String inputFile)
inputFile
- to read to determine the line-terminator.
public static java.lang.String getDirPathByDirBrowser(java.lang.String defaultDir, java.lang.String sPrompt)
defaultDir
- if not nullsPrompt
- to use if not null
public boolean testFile(java.lang.String filePath)
filePath
- to test
public boolean testDir(java.lang.String dirPath)
dirPath
- to test
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |