fr.tireme.shared
Class FileUtils

java.lang.Object
  extended by fr.tireme.shared.FileUtils

public class FileUtils
extends java.lang.Object

File utilities Note: a lot of the provided code here has been extracted and modified from the org.apache.tools.ant.util.FileUtils and org.apache.tools.ant.taskdefs.Replace classes.

Since:
16 novembre 2003
Author:
pierre

Field Summary
static java.lang.String LINE_SEP
          the line separator for this OS
 
Constructor Summary
FileUtils()
          Constructor for non static methods
 
Method Summary
 java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File parentDir)
          Create a temporary file in a given directory.
static void fileCopy(java.io.File from, java.io.File to)
          Copy a file onto the filesystem
static void fileCopy(java.lang.String from, java.lang.String to)
          Copy a file onto the filesystem
 boolean fileNameEquals(java.io.File f1, java.io.File f2)
          Compares two filenames.
 java.net.URL getFileURL(java.io.File file)
          Get the URL for a file taking into account # characters
 java.io.File getParentFile(java.io.File f)
          Emulation of File.getParentFile for JDK 1.1
static java.io.File getTempDir()
          Get the temporary directory
static java.io.File makeFile(java.io.File mainFileDir, java.lang.String href)
          Creates
static java.io.File normalize(java.lang.String path)
          "normalize" the given absolute path.
static java.lang.String normalizeAndValidateFile(java.lang.String fileName)
          Normalize a file according to system specificities and validate if exists
static java.lang.String normalizeAndValidateFile(java.lang.String fileName, boolean asURL)
          Normalize a file according to system specificities and validate if exists
static java.lang.String normalizeFile(java.lang.String fileName)
          Normalize a file according to system specificities
static void printToFile(java.lang.String result, java.lang.String fileName)
           
 java.lang.StringBuffer readFile(java.lang.String confPath)
          readFile gets the StringBuffer of a file from the passed file path.
 void rename(java.io.File from, java.io.File to)
          Renames a file, even if that involves crossing file system boundaries.
 void replaceStringInFile(java.io.File src, java.lang.String token, java.lang.String value)
          Perform replacement on a given file.
 boolean storeFile(java.lang.String filePath, java.lang.String file)
          storeFile takes a path name plus a String content and creates the corresponding file. important: if the file already exists, its content will be overwritten by the new content
static java.lang.String toURI(java.lang.String path)
          Constructs a file: URI that represents the external form of the given pathname.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEP

public static final java.lang.String LINE_SEP
the line separator for this OS

Constructor Detail

FileUtils

public FileUtils()
Constructor for non static methods

Method Detail

fileCopy

public static void fileCopy(java.io.File from,
                            java.io.File to)
                     throws StandardException
Copy a file onto the filesystem

Parameters:
from - source path
to - destination path
Throws:
StandardException - All exceptions found

fileCopy

public static void fileCopy(java.lang.String from,
                            java.lang.String to)
                     throws StandardException
Copy a file onto the filesystem

Parameters:
from - source path
to - destination path
Throws:
StandardException - All exceptions found

getTempDir

public static java.io.File getTempDir()
Get the temporary directory

Returns:
the file created

makeFile

public static java.io.File makeFile(java.io.File mainFileDir,
                                    java.lang.String href)
                             throws java.io.FileNotFoundException,
                                    java.io.IOException
Creates

Parameters:
mainFileDir - File
href - String
Returns:
java.io.File
Throws:
java.io.FileNotFoundException
java.io.IOException

normalize

public static java.io.File normalize(java.lang.String path)
                              throws StandardException
"normalize" the given absolute path.

This includes:

Unlike File#getCanonicalPath() it specifically doesn't resolve symbolic links.

Parameters:
path - the path to be normalized
Returns:
the normalized version of the path.
Throws:
StandardException - if the file path is equal to null.

normalizeAndValidateFile

public static java.lang.String normalizeAndValidateFile(java.lang.String fileName)
                                                 throws StandardException
Normalize a file according to system specificities and validate if exists

Parameters:
fileName - file name to be normalized
Returns:
normalized file name
Throws:
StandardException - raised if file does not exists.

normalizeAndValidateFile

public static java.lang.String normalizeAndValidateFile(java.lang.String fileName,
                                                        boolean asURL)
                                                 throws StandardException
Normalize a file according to system specificities and validate if exists

Parameters:
fileName - file name to be normalized
asURL - want result as an URI
Returns:
normalized file name
Throws:
StandardException - raised if file does not exists.

normalizeFile

public static java.lang.String normalizeFile(java.lang.String fileName)
                                      throws StandardException
Normalize a file according to system specificities

Parameters:
fileName - file name to be normalized
Returns:
normalized file name
Throws:
StandardException - Description of Exception

printToFile

public static void printToFile(java.lang.String result,
                               java.lang.String fileName)
                        throws StandardException
Parameters:
result - data to be printed
fileName - Description of Parameter
Throws:
StandardException - All exceptions found

toURI

public static java.lang.String toURI(java.lang.String path)
Constructs a file: URI that represents the external form of the given pathname.

Will be an absolute URI if the given path is absolute.

This code doesn't handle non-ASCII characters properly.

Parameters:
path - the path in the local file system
Returns:
the URI version of the local path.
Since:
Ant 1.6

createTempFile

public java.io.File createTempFile(java.lang.String prefix,
                                   java.lang.String suffix,
                                   java.io.File parentDir)
Create a temporary file in a given directory.

The file denoted by the returned abstract pathname did not exist before this method was invoked, any subsequent invocation of this method will yield a different file name.

This method is different to File.createTempFile of JDK 1.2 as it doesn't create the file itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is null.

Parameters:
prefix - prefix to add to filename
suffix - suffix to add to filename
parentDir - Directory to create the temporary file in - current working directory will be assumed if this parameter is null.
Returns:
a File reference to the new temporary file.

fileNameEquals

public boolean fileNameEquals(java.io.File f1,
                              java.io.File f2)
                       throws StandardException
Compares two filenames.

Unlike java.io.File#equals this method will try to compare the absolute paths and "normalize" the filenames before comparing them.

Parameters:
f1 - the file whose name is to be compared.
f2 - the other file whose name is to be compared.
Returns:
true if the file are for the same file.
Throws:
StandardException

getFileURL

public java.net.URL getFileURL(java.io.File file)
                        throws java.net.MalformedURLException
Get the URL for a file taking into account # characters

Parameters:
file - the file whose URL representation is required.
Returns:
The FileURL value
Throws:
java.net.MalformedURLException - if the URL representation cannot be formed.

getParentFile

public java.io.File getParentFile(java.io.File f)
Emulation of File.getParentFile for JDK 1.1

Parameters:
f - the file whose parent is required.
Returns:
the given file's parent, or null if the file does not have a parent.
Since:
1.10

readFile

public java.lang.StringBuffer readFile(java.lang.String confPath)
                                throws java.io.FileNotFoundException
readFile gets the StringBuffer of a file from the passed file path.

Parameters:
confPath - file's path
Returns:
StringBuffer - the loaded StringBuffered file or null
Throws:
java.io.FileNotFoundException

rename

public void rename(java.io.File from,
                   java.io.File to)
            throws java.io.IOException,
                   StandardException
Renames a file, even if that involves crossing file system boundaries.

This will remove to (if it exists), ensure that to's parent directory exists and move from, which involves deleting from as well.

Parameters:
from - the file to move
to - the new file name
Throws:
java.io.IOException - if anything bad happens during this process. Note that to may have been deleted already when this happens.
StandardException

replaceStringInFile

public void replaceStringInFile(java.io.File src,
                                java.lang.String token,
                                java.lang.String value)
                         throws StandardException
Perform replacement on a given file. The replacement is performed on a temporary file which then replaces the original file.

Parameters:
src - the source file
token - string to search
value - replacement string
Throws:
StandardException

storeFile

public boolean storeFile(java.lang.String filePath,
                         java.lang.String file)
storeFile takes a path name plus a String content and creates the corresponding file. important: if the file already exists, its content will be overwritten by the new content

Parameters:
filePath - file's full path
file - file's content
Returns:
was it OK ?