net.sourceforge.cobertura.javancss

Class FileUtil


public class FileUtil
extends java.lang.Object

Utility class for file operations.

Simple but most commonly used methods of this class are:
- readFile
- concatPath
Other less frequently used but still handy methods are:
- normalizeFileName to take the current user directory into account via the 'user.dir' system property

Version:
$Id: FileUtil.java 384 2006-03-17 20:10:49Z thekingant $
Author:
Chr. Clemens Lee < clemens@kclee.com >

Constructor Summary

FileUtil()
Utility class which should never instanciate itself.

Method Summary

private static String
concatPath(String sPath_, String sFile_)
Concatenates a file path with the file name.
private static String
getAbsoluteFileName(String sFileName_)
private static boolean
isAbsolute(String sFileName_)
Tests if the file represented by this File object is an absolute pathname.
static String
normalizeFileName(String sFile)
This method returns an absolute (canonical) file name.
private static String
normalizeFileName(String sFile, String sUserDir)
This method returns an absolute (canonical) file name.
static String
readFile(String sFileName_)
Reads a File and returns the content in a String.

Constructor Details

FileUtil

private FileUtil()
Utility class which should never instanciate itself.

Method Details

concatPath

private static String concatPath(String sPath_,
                                 String sFile_)
Concatenates a file path with the file name. If necessary it adds a File.separator between the path and file name. For example "/home" or "/home/" and "clemens" both become "/home/clemens".

This method is inspired from the FrIJDE project out of the gCollins.File.FileTools class.

FrIJDE Homepage: http://amber.wpi.edu/~thethe/Document/Besiex/Java/FrIJDE/

Parameters:
sPath_ - a directory path. Is not allowed to be null.
sFile_ - the base name of a file.
Returns:
sPath_ if sFile_ is empty.

getAbsoluteFileName

private static String getAbsoluteFileName(String sFileName_)
Returns:
It's the canonical path of sFileName_.

isAbsolute

private static boolean isAbsolute(String sFileName_)
Tests if the file represented by this File object is an absolute pathname. The definition of an absolute pathname is system dependent. For example, on UNIX, a pathname is absolute if its first character is the separator character. On Windows platforms, a pathname is absolute if its first character is an ASCII '\' or '/', or if it begins with a letter followed by a colon.

normalizeFileName

public static String normalizeFileName(String sFile)
This method returns an absolute (canonical) file name. The difference to getAbsoluteFileName is that this method uses the system property "user.dir" instead of the native system's current directory. This way you get a chance of changing the current directory inside Java and let your program reflect that change.

normalizeFileName

private static String normalizeFileName(String sFile,
                                        String sUserDir)
This method returns an absolute (canonical) file name. The difference to getAbsoluteFileName is that this method uses the system property sUserDir instead of the native system's current directory. This way you get a chance of changing the current directory inside Java and let your program reflect that change.

readFile

public static String readFile(String sFileName_)
            throws IOException,
                   FileNotFoundException
Reads a File and returns the content in a String. CRLF -> LF conversion takes place. This is a convenience method so you don't need to bother creating a file reader object and closing it after it has been used.
Parameters:
sFileName_ - the name of the file to read.
Returns:
a string with the content of the file but without any CR characters.