Package fmpp.util
Class FileUtil
- java.lang.Object
-
- fmpp.util.FileUtil
-
public class FileUtil extends java.lang.Object
Collection of file and path related functions.
-
-
Constructor Summary
Constructors Constructor Description FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
compressPath(java.lang.String path, int maxPathLength)
Returns a compressed version of the path.static void
copyFile(java.io.File src, java.io.File dst)
Same ascopyFile(src, dst, true))
.static void
copyFile(java.io.File src, java.io.File dst, boolean copyLMD)
Copies a file; silently overwrites the destination if already exists.static java.lang.String
getFileExtension(java.lang.String name)
Returns the part of the name after the last dot, or if there's no dot,null
.static java.lang.String
getLowerCaseFileExtension(java.lang.String name)
Same asgetFileExtension(String)
, but also converts the result to lower case.static java.lang.String
getRelativePath(java.io.File fromDir, java.io.File toFileOrDir)
Reaturns the path of a file or directory relative to a directory, in native format.static boolean
isInside(java.io.File file, java.io.File ascendant)
Returns true iffile
is insideascendant
, otherwise returns false.static boolean
isInsideOrEquals(java.io.File file, java.io.File ascendant)
Returns true iffile
is insideascendant
orfile
is the same as theascendant
, otherwise returns false.static byte[]
loadByteArray(java.io.InputStream in)
static java.lang.String
loadString(java.io.InputStream in, java.lang.String charset)
static java.lang.String
pathPatternToPerl5Regex(java.lang.String text)
Converts UN*X style path to regular expression (originally, for Perl 5 dialect, but also works for Java's dialect).static java.lang.String
pathToUnixStyle(java.lang.String path)
Brings the path to UNI*X style format, so that it can be handled with path pattern handling functions.static java.lang.String
removeSlashPrefix(java.lang.String path)
static java.io.File
resolveRelativeUnixPath(java.io.File root, java.io.File wd, java.lang.String path)
Resolves relative UN*X path based on given root and working directory.
-
-
-
Method Detail
-
getRelativePath
public static java.lang.String getRelativePath(java.io.File fromDir, java.io.File toFileOrDir) throws java.io.IOException
Reaturns the path of a file or directory relative to a directory, in native format.- Returns:
- The relative path. It never starts with separator char (/ on UN*X).
- Throws:
java.io.IOException
- if the two paths has no common parent directory (such asC:\foo.txt
andD:\foo.txt
), or the the paths are malformed.
-
copyFile
public static void copyFile(java.io.File src, java.io.File dst, boolean copyLMD) throws java.io.IOException
Copies a file; silently overwrites the destination if already exists.- Parameters:
copyLMD
- tells if the last modification time of the original file will be copied too.- Throws:
java.io.IOException
-
copyFile
public static void copyFile(java.io.File src, java.io.File dst) throws java.io.IOException
Same ascopyFile(src, dst, true))
.- Throws:
java.io.IOException
-
isInsideOrEquals
public static boolean isInsideOrEquals(java.io.File file, java.io.File ascendant)
Returns true iffile
is insideascendant
orfile
is the same as theascendant
, otherwise returns false.
-
isInside
public static boolean isInside(java.io.File file, java.io.File ascendant)
Returns true iffile
is insideascendant
, otherwise returns false.
-
resolveRelativeUnixPath
public static java.io.File resolveRelativeUnixPath(java.io.File root, java.io.File wd, java.lang.String path) throws java.io.IOException
Resolves relative UN*X path based on given root and working directory.- Parameters:
root
- root directorywd
- working directory (current direcory)- Throws:
java.io.IOException
-
compressPath
public static java.lang.String compressPath(java.lang.String path, int maxPathLength)
Returns a compressed version of the path. For example,/foo/ba.../baaz.txt
instead of/foo/bar/blah/blah/blah/baaz.txt
.- Parameters:
path
- the path to compress. Either native or UNIX format.maxPathLength
- the maximum length of the result. Must be at least 4.
-
pathToUnixStyle
public static java.lang.String pathToUnixStyle(java.lang.String path)
Brings the path to UNI*X style format, so that it can be handled with path pattern handling functions.
-
removeSlashPrefix
public static java.lang.String removeSlashPrefix(java.lang.String path)
-
getFileExtension
public static java.lang.String getFileExtension(java.lang.String name)
Returns the part of the name after the last dot, or if there's no dot,null
.- Parameters:
name
- The file name or path. Ifnull
,null
is returned.- Since:
- 0.9.16
-
getLowerCaseFileExtension
public static java.lang.String getLowerCaseFileExtension(java.lang.String name)
Same asgetFileExtension(String)
, but also converts the result to lower case.- Since:
- 0.9.16
-
pathPatternToPerl5Regex
public static java.lang.String pathPatternToPerl5Regex(java.lang.String text)
Converts UN*X style path to regular expression (originally, for Perl 5 dialect, but also works for Java's dialect). In additional to standard UN*X path meta characters (*
,?
) it understands**
, that is the same as in Ant. It assumes that the paths what you will later match with the pattern are always starting with slash (they are absolute paths to an imaginary base).
-
loadString
public static java.lang.String loadString(java.io.InputStream in, java.lang.String charset) throws java.io.IOException
- Throws:
java.io.IOException
-
loadByteArray
public static byte[] loadByteArray(java.io.InputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
-