com.dynamide.util
Class FileTools

java.lang.Object
  extended by com.dynamide.util.FileTools
All Implemented Interfaces:
IFileDiverListener

public class FileTools
extends java.lang.Object
implements IFileDiverListener


Nested Class Summary
static class FileTools.FileInfo
          A class that contains information about a file and its contents so that the class can be read fully, while preserving the File handle to pass back to the caller.
 
Field Summary
static int LN_FILENAME
           
static int LN_FILENAME_NO_EXT
           
static int LN_FULL
           
static int LT_ALL
           
static int LT_DIRS
           
static int LT_FILES
           
 
Constructor Summary
FileTools()
           
 
Method Summary
static boolean backup(java.lang.String fileName)
          Copies the file named by fileName to a backup file in the same directory, with a name generated by FileTools.generateBackupName(fileName) and returns true, otherwise returns true, and returns false if there's an Exception.
static void cleanOneDir(java.lang.String dir, java.lang.String name, java.lang.String sanity)
           
static java.util.List copyDir(java.lang.String srcDir, java.lang.String destDir)
          Copy srcDir to destDir, overwriting all files.
static boolean copyFile(java.lang.String sourceFileName, java.lang.String destFileName)
          Copy a binary source file to a destination file.
static java.lang.String copyToTempFile(java.lang.String directory, java.lang.String shortFilename, java.lang.String extension)
          Finds a new name for the file named in the directory named, and creates that file, without deleting the original file.
static java.io.File createDirectory(java.lang.String fullPath)
           
static java.io.File createDirectory(java.lang.String baseDir, java.lang.String relativePath)
          Creates a directory, returned as a File object -- call File.getCanonicalPath() to see the full path as a String().
static java.io.File createDirectory(java.lang.String baseDir, java.lang.String relativePath, boolean nullReturnIfDirExists)
           
static java.io.File createTempSubdirectory(java.lang.String leader)
           
static boolean delete(java.lang.String filename)
           
static boolean delete(java.lang.String dirname, java.lang.String filename)
           
static void directoryDiver(java.lang.String diveID, java.io.File directory, java.lang.String relativePath, IFileDiverListener listener, java.lang.String separator)
           
static void directoryDiver(java.lang.String diveID, java.lang.String startingDir, IFileDiverListener listener)
           
static void directoryDiver(java.lang.String diveID, java.lang.String startingDir, IFileDiverListener listener, java.lang.String separator)
           
static boolean directoryExists(java.lang.String filename)
           
static boolean fileExists(java.lang.String filename)
           
static boolean fileSystemIsDOS()
           
static java.util.List<java.lang.String> fileToLines(java.io.File file)
           
static java.lang.String fixFilename(java.lang.String filename)
           
static java.lang.String generateBackupName(java.lang.String basename)
           
static boolean isWebSafeFileName(java.lang.String path)
           
static java.lang.String join(java.lang.String dir, java.lang.String file)
           
static java.lang.String joinExt(java.lang.String filename, java.lang.String ext)
           
static java.lang.String joinExt(java.lang.String filename, java.lang.String ext, boolean isURI)
           
static java.lang.String joinURI(java.lang.String dir, java.lang.String file)
           
static java.lang.String joinURIExt(java.lang.String filename, java.lang.String ext)
           
static java.lang.String joinURIExt(java.lang.String dir, java.lang.String filename, java.lang.String ext)
           
static java.util.Map list(java.lang.String dirname, int type, int nameStyle)
           
static java.util.Properties loadPropertiesFromFile(java.lang.String dir, java.lang.String relativeName)
           
static void main(java.lang.String[] args)
           
 boolean onDirectory(java.lang.String diveID, java.io.File directory, java.lang.String dirname, java.lang.String relativePath)
           
 void onFile(java.lang.String diveID, java.io.File directory, java.io.File file, java.lang.String filename, java.lang.String relativePath)
          filename is the short name of the file, without directory info, relativePath is the relative path, including intermediate directories, starting from the point where the dive was initiated.
static java.io.File openFile(java.lang.String dir, java.lang.String relPath)
           
static java.io.File openFile(java.lang.String dir, java.lang.String relPath, boolean forceParentDirectories)
           
static byte[] readBinaryFile(java.lang.String dir, java.lang.String relPath)
           
static FileTools.FileInfo readBinaryFileInfo(java.lang.String dir, java.lang.String relPath)
           
static FileTools.FileInfo readBinaryFileInfo(java.lang.String dir, java.lang.String relPath, long lastModified)
           
static java.lang.String readFile(java.lang.String fullPath)
           
static java.lang.String readFile(java.lang.String dir, java.lang.String relPath)
           
static FileTools.FileInfo readFileInfo(java.lang.String dir, java.lang.String relPath)
           
static FileTools.FileInfo readFileInfo(java.lang.String dir, java.lang.String relPath, long lastModified)
           
static java.lang.String readFully(java.io.InputStream inputStream)
           
static java.lang.String resolve(java.lang.String path)
           
static java.io.File saveFile(java.lang.String dir, java.lang.String relativeName, byte[] content)
           
static java.io.File saveFile(java.lang.String dir, java.lang.String relativeName, java.lang.String content)
           
static boolean savePropertiesToFile(java.util.Properties props, java.lang.String headerComment, java.lang.String dir, java.lang.String relativeName)
           
static java.io.File saveToTempFile(java.lang.String directory, java.lang.String content)
           
static java.lang.String toURI(java.lang.String filename)
           
static void usage()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LT_FILES

public static final int LT_FILES
See Also:
Constant Field Values

LT_DIRS

public static final int LT_DIRS
See Also:
Constant Field Values

LT_ALL

public static final int LT_ALL
See Also:
Constant Field Values

LN_FULL

public static final int LN_FULL
See Also:
Constant Field Values

LN_FILENAME

public static final int LN_FILENAME
See Also:
Constant Field Values

LN_FILENAME_NO_EXT

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

FileTools

public FileTools()
Method Detail

directoryDiver

public static void directoryDiver(java.lang.String diveID,
                                  java.lang.String startingDir,
                                  IFileDiverListener listener)

directoryDiver

public static void directoryDiver(java.lang.String diveID,
                                  java.lang.String startingDir,
                                  IFileDiverListener listener,
                                  java.lang.String separator)

directoryDiver

public static void directoryDiver(java.lang.String diveID,
                                  java.io.File directory,
                                  java.lang.String relativePath,
                                  IFileDiverListener listener,
                                  java.lang.String separator)

onFile

public void onFile(java.lang.String diveID,
                   java.io.File directory,
                   java.io.File file,
                   java.lang.String filename,
                   java.lang.String relativePath)
filename is the short name of the file, without directory info, relativePath is the relative path, including intermediate directories, starting from the point where the dive was initiated. So if you called directoryDiver("/dynamide", this), you could expect to see a file whose full path was /dynamide/src/conf/log.conf be reported as "src/conf/log.conf".

Specified by:
onFile in interface IFileDiverListener
Parameters:
diveID - This identifies the callback so that you know which thing you are doing if you use the callback interface for two tasks within one class, or within multiple threads.
See Also:
directoryDiver(java.lang.String, java.lang.String, com.dynamide.util.IFileDiverListener)

onDirectory

public boolean onDirectory(java.lang.String diveID,
                           java.io.File directory,
                           java.lang.String dirname,
                           java.lang.String relativePath)
Specified by:
onDirectory in interface IFileDiverListener
Parameters:
diveID - This identifies the callback so that you know which thing you are doing if you use the callback interface for two tasks within one class, or within multiple threads.
Returns:
true if you wish to continue diving INTO the named directory, otherwise return false to skip the contents of the named directory.
See Also:
for a description of relativePath.

list

public static java.util.Map list(java.lang.String dirname,
                                 int type,
                                 int nameStyle)
                          throws java.lang.Exception
Parameters:
type - should be one of LT_FILES, LT_DIRS or LT_ALL.
nameStyle - should be one of LN_FULL, LN_FILENAME or LN_FILENAME_NO_EXT.
Throws:
java.lang.Exception

fileSystemIsDOS

public static boolean fileSystemIsDOS()

fixFilename

public static java.lang.String fixFilename(java.lang.String filename)

join

public static java.lang.String join(java.lang.String dir,
                                    java.lang.String file)

joinURI

public static java.lang.String joinURI(java.lang.String dir,
                                       java.lang.String file)

toURI

public static java.lang.String toURI(java.lang.String filename)

joinExt

public static java.lang.String joinExt(java.lang.String filename,
                                       java.lang.String ext)

joinURIExt

public static java.lang.String joinURIExt(java.lang.String filename,
                                          java.lang.String ext)

joinURIExt

public static java.lang.String joinURIExt(java.lang.String dir,
                                          java.lang.String filename,
                                          java.lang.String ext)

joinExt

public static java.lang.String joinExt(java.lang.String filename,
                                       java.lang.String ext,
                                       boolean isURI)

createDirectory

public static java.io.File createDirectory(java.lang.String fullPath)

createDirectory

public static java.io.File createDirectory(java.lang.String baseDir,
                                           java.lang.String relativePath)
Creates a directory, returned as a File object -- call File.getCanonicalPath() to see the full path as a String().

Parameters:
relativePath - can be empty.
baseDir - can be be a full path or partial path.
Returns:
a File object pointing at the new directory if successful, else null.

createDirectory

public static java.io.File createDirectory(java.lang.String baseDir,
                                           java.lang.String relativePath,
                                           boolean nullReturnIfDirExists)

createTempSubdirectory

public static java.io.File createTempSubdirectory(java.lang.String leader)
                                           throws java.util.ConcurrentModificationException
Throws:
java.util.ConcurrentModificationException

delete

public static boolean delete(java.lang.String filename)

delete

public static boolean delete(java.lang.String dirname,
                             java.lang.String filename)

cleanOneDir

public static void cleanOneDir(java.lang.String dir,
                               java.lang.String name,
                               java.lang.String sanity)
Parameters:
sanity - is a safety check, or you can pass "".

isWebSafeFileName

public static boolean isWebSafeFileName(java.lang.String path)

copyFile

public static boolean copyFile(java.lang.String sourceFileName,
                               java.lang.String destFileName)
Copy a binary source file to a destination file. Filenames are platform dependent. Returns true for copying a zero byte file. Returns true if the file copy was succesful.


copyDir

public static java.util.List copyDir(java.lang.String srcDir,
                                     java.lang.String destDir)
                              throws java.io.IOException
Copy srcDir to destDir, overwriting all files. No files or dirs in destDir are removed if not present in srcDir. destDir may exist. "CVS" directories are skipped.

Throws:
java.io.IOException

openFile

public static java.io.File openFile(java.lang.String dir,
                                    java.lang.String relPath)

openFile

public static java.io.File openFile(java.lang.String dir,
                                    java.lang.String relPath,
                                    boolean forceParentDirectories)

resolve

public static java.lang.String resolve(java.lang.String path)
                                throws java.io.IOException
Throws:
java.io.IOException

readFile

public static java.lang.String readFile(java.lang.String fullPath)

readFile

public static java.lang.String readFile(java.lang.String dir,
                                        java.lang.String relPath)

readFileInfo

public static FileTools.FileInfo readFileInfo(java.lang.String dir,
                                              java.lang.String relPath)

readFileInfo

public static FileTools.FileInfo readFileInfo(java.lang.String dir,
                                              java.lang.String relPath,
                                              long lastModified)
Parameters:
lastModified - Pass zero to indicate that you want the file read regardless of the last modified timestamp, otherwise this method compares the value with the File's lastModified() result, and returns an empty FileInfo with getNotModified() == true, or a filled out FileInfo if the file has been modified since the timestamp.

readBinaryFile

public static byte[] readBinaryFile(java.lang.String dir,
                                    java.lang.String relPath)

readBinaryFileInfo

public static FileTools.FileInfo readBinaryFileInfo(java.lang.String dir,
                                                    java.lang.String relPath)

readBinaryFileInfo

public static FileTools.FileInfo readBinaryFileInfo(java.lang.String dir,
                                                    java.lang.String relPath,
                                                    long lastModified)

readFully

public static java.lang.String readFully(java.io.InputStream inputStream)
                                  throws java.io.IOException
Throws:
java.io.IOException

saveToTempFile

public static java.io.File saveToTempFile(java.lang.String directory,
                                          java.lang.String content)

copyToTempFile

public static java.lang.String copyToTempFile(java.lang.String directory,
                                              java.lang.String shortFilename,
                                              java.lang.String extension)
                                       throws java.lang.Exception
Finds a new name for the file named in the directory named, and creates that file, without deleting the original file.

Parameters:
directory - The canonical path to the file, without filename or extension
shortFilename - the basename of the file without extension, e.g. for /tmp/MyFile.txt shortFilename is "MyFile"
extension - Just the extension, without the dot, eg. for /tmp/MyFile.txt extension is "txt"
Returns:
the handle to the newly copied file.
Throws:
java.lang.Exception

saveFile

public static java.io.File saveFile(java.lang.String dir,
                                    java.lang.String relativeName,
                                    java.lang.String content)
Returns:
The File object if successful, otherwise null.

saveFile

public static java.io.File saveFile(java.lang.String dir,
                                    java.lang.String relativeName,
                                    byte[] content)

generateBackupName

public static java.lang.String generateBackupName(java.lang.String basename)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

backup

public static boolean backup(java.lang.String fileName)
Copies the file named by fileName to a backup file in the same directory, with a name generated by FileTools.generateBackupName(fileName) and returns true, otherwise returns true, and returns false if there's an Exception.


fileExists

public static boolean fileExists(java.lang.String filename)
Returns:
false if file is a directory or file not found.

directoryExists

public static boolean directoryExists(java.lang.String filename)
Returns:
false if file is a not a directory or is not found.

loadPropertiesFromFile

public static java.util.Properties loadPropertiesFromFile(java.lang.String dir,
                                                          java.lang.String relativeName)
Returns:
null if file not found, etc.

savePropertiesToFile

public static boolean savePropertiesToFile(java.util.Properties props,
                                           java.lang.String headerComment,
                                           java.lang.String dir,
                                           java.lang.String relativeName)
Parameters:
headerComment - can be null, if no # comment is desired at the top of the file.

fileToLines

public static java.util.List<java.lang.String> fileToLines(java.io.File file)
                                                    throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

usage

public static void usage()

main

public static void main(java.lang.String[] args)


Copyright 2001-2013 DYNAMIDE.COM. All Rights Reserved.