public class FileFunctions
extends java.lang.Object
getDirectory(File)
returns the directory or null
if the input file is not existing. It does not throw a FileNotFoundException. The result is expected, the user
can do a null-check easily.Modifier and Type | Class and Description |
---|---|
static interface |
FileFunctions.AddFileToList |
static class |
FileFunctions.FileAndBasePath
This class holds a File with its Basepath.
|
private static class |
FileFunctions.FilesWithBasePath
Temporary class used only inside
addFilesWithBasePath(java.io.File, java.lang.String, java.util.List<org.vishia.util.FileFunctions.FileAndBasePath>) . |
private static class |
FileFunctions.ListWrapper
This class supports the call of
addFileToList(String, List) . |
private static class |
FileFunctions.WildcardFilter
Filter for a file path maybe with wildcards in the directory path.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
sVersion
Version, history and license.
|
Constructor and Description |
---|
FileFunctions() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
absolutePath(java.lang.String sFileNameP,
java.io.File currDir)
Converts to the absolute and normalized path if a relative path or HOME path is given.
|
static boolean |
addFilesWithBasePath(java.io.File baseDir,
java.lang.String sPathArg,
java.util.List<FileFunctions.FileAndBasePath> list)
Fills the list with found files to sPath.
|
private static boolean |
addFileToList(FileFunctions.AddFileToList listFiles,
java.io.File dir,
java.lang.String sPath,
int posWildcard,
java.io.FilenameFilter filterName,
java.io.FilenameFilter filterAlldir,
int recursivect)
Executes adding file to the given list.
|
static boolean |
addFileToList(java.io.File dir,
java.lang.String sPath,
FileFunctions.AddFileToList listFiles)
Add files
|
static boolean |
addFileToList(java.io.File dir,
java.lang.String sPath,
java.util.List<java.io.File> listFiles)
Add files.
|
static boolean |
addFileToList(java.lang.String sPath,
FileFunctions.AddFileToList listFiles)
Adds Files with the wildcard-path to a given list.
|
static boolean |
addFileToList(java.lang.String sPath,
java.util.List<java.io.File> listFiles)
Adds Files with the wildcard-path to a given list.
|
private static java.io.File |
buildDir(java.io.File dirParent,
java.lang.String sPath,
int posFile)
Builds a File which is a directory of
|
static int |
checkNewless(java.io.File src,
java.io.File dst,
boolean removeDstIfOlder)
Checks whether a file is newer as the other, maybe delete dst.
|
static java.lang.String |
cleanAbsolutePath(java.lang.String inp)
Cleans ".." and "." from an absolute path.
|
static boolean |
cleandir(java.io.File dir_file)
Removes all files inside the directory and all sub directories with its files.
|
static boolean |
cleandirForced(java.io.File dir_file)
Removes all files inside the directory and all sub directories with its files.
|
static boolean |
close(java.io.Closeable file)
Close operation without exception.
|
static boolean |
copyDir(java.io.File src,
java.io.File dst)
Removes all files inside the directory and all sub directories with its files.
|
static int |
copyFile(java.io.File src,
java.io.File dst)
Copy a file.
|
static int |
copyFile(java.io.File src,
java.io.File dst,
boolean bKeepTimestamp,
boolean bOverwrite,
boolean bOverwriteReadonly)
Copy a file.
|
static boolean |
delete(java.lang.String path) |
static boolean |
exists(java.io.File file)
Returns true if the given file exists.
|
static java.lang.String |
getCanonicalPath(java.io.File file)
Gets the canonical path of a file without exception and with unique slashes.
|
static java.io.File |
getDir(java.io.File file)
Returns the directory of the given file.
|
static java.io.File |
getDirectory(java.io.File file)
Returns the directory of the given file.
|
static java.io.File[] |
getFiles(java.io.File dir,
java.lang.CharSequence nameWithWildcard)
Returns all matching files of the directory.
|
static java.io.File |
getFirstFileWildcard(java.io.File file)
Returns the first file with the matching name in the given directory.
|
static java.lang.String |
grep1line(java.io.File file,
java.lang.String what)
Searches the first line with given text in 1 file, returns the line or null.
|
static boolean |
isAbsolutePath(java.lang.CharSequence filePath)
Returns true if the String which describes a file path is recognized as an absolute path.
|
static boolean |
isAbsolutePathOrDrive(java.lang.CharSequence filePath)
Returns true if the String which describes a file path is recognized as an absolute path or as a path with drive letter
which may be relative on the drive.
|
static boolean |
isRoot(java.lang.CharSequence sName) |
static boolean |
isRoot(java.io.File file)
Returns true if this file describes a root directory.
|
static boolean |
isSymbolicLink(java.io.File file)
Returns true if the file is symbolic linked.
|
static java.io.File |
mkDir(java.io.File dir)
checks if the directory path exists or execute mkdir for all not existing directory levels.
|
static void |
mkDirPath(java.io.File file)
checks if a path exists or execute mkdir for all not existing directory levels.
|
static java.io.File |
mkDirPath(java.lang.String sPath)
checks if a path exists or execute mkdir for all not existing directory levels.
|
static java.lang.CharSequence |
normalizePath(java.lang.CharSequence inp)
Cleans any /../ and /./ from a path, it makes it normalized or canonical.
|
static java.lang.CharSequence |
normalizePath(java.io.File file)
Returns the normalized absolute path from a file.
|
static byte[] |
readBinFile(java.io.File file)
Reads the content of a whole file into a byte array.
|
static int |
readBinFile(java.io.File file,
byte[] buffer)
Reads the content of a file into a byte array.
|
static java.lang.String |
readFile(java.io.File file)
Reads the content of a whole file into a String.
|
static java.lang.String |
readFile(java.io.File file,
java.lang.Appendable dst,
int[] zdst)
Reads the content of a whole file into a Appendable dst.
|
static java.lang.CharSequence |
readInJar(java.lang.Class clazz,
java.lang.String pathInJar,
java.lang.String encoding)
Read a file ('Resource') located in a jar file or inside the compiled class files.
|
static java.lang.String |
relativatePath(java.lang.String sInput,
java.lang.String sRefPath)
Builds a relative path from a given directory, from an input path and the reference directory.
|
static boolean |
renameCreate(java.io.File dir,
java.lang.String src,
java.lang.String dst,
boolean bException)
Renames from src or creates the dst file.
|
static boolean |
rmdir(java.io.File dir)
Removes all files inside the directory and all sub directories with its files.
|
static java.io.File |
searchFileInParent(java.io.File start,
java.lang.String... pattern)
Searches a file given with local path in this directory and in all parent directories.
|
static java.lang.String[] |
searchInFiles(java.util.List<java.io.File> files,
java.lang.String what,
java.lang.Appendable searchOutput)
This is equal the usual grep, but with given files.
|
static java.io.File |
searchInParent(java.io.File start,
java.lang.String... path)
Deprecated.
not currently tested, use
searchFileInParent(File, String...) for simple files with wildcard-mask. |
static boolean |
setCurrdirFromFile(java.lang.String file)
Sets the current dir from the stored String in a file.
|
private static void |
stop() |
static int |
writeBinFile(java.io.File file,
byte[] buffer)
Writes the content of a whole file from a byte array.
|
static boolean |
writeFile(java.lang.String content,
java.io.File file)
Writes the given String as content to a file without exception throwing.
|
static boolean |
writeFile(java.lang.String content,
java.lang.String sFile)
Writes the given String as content to a file without exception throwing.
|
public static final java.lang.String sVersion
FileSystem
too. Prevent name clash.
absolutePath(String, File)
refactored, some problems on Zip
with jar.
readFile(File, Appendable, int[])
into a buffer.
#searchFileInParent(File, String...))
used for git GUI
to search the .git or *.gitRepository from an inner file in the working area.
readInJar(Class, String, String)
as simple read possibility of a text file.
cleandirForced(File)
, copyDir(File, File)
getFirstFileWildcard(File)
FilepathFilter
used.
copyFile(File, File, boolean, boolean, boolean)
readFile(File)
searchInParent(File, String...)
searchInFiles(List, String, Appendable)
missing close().
grep1line(File, String)
: Should close the file. Nobody does it elsewhere.
normalizePath(CharSequence)
: it has not deleted
path/folder/folder/../../ because faulty following start search position.
- 2014-05-10 Hartmut new:
delete(String)
- 2013-10-27 Hartmut chg:
normalizePath(CharSequence)
now uses a given StringBuilder to adjust the path
inside itself. normalizePath(myStringBuilder) does not need the return value.
But normalizePath(myStringBuilder.toString()) normalizes in a new StringBuilder.
Rule: A CharSequence can't be seen as persistent. Only a String as CharSequence is persistent.
- 2013-08-29 Hartmut bugfix:
normalizePath(CharSequence)
, isAbsolutePath(CharSequence)
- 2013-06-27 Hartmut new:
close(Closeable)
- 2013-05-04 Hartmut chg:
normalizePath(CharSequence)
uses and returns a CharSequence yet.
- 2013-03-31 Hartmut bugfix:
addFileToList(AddFileToList, File, String, int, FilenameFilter, FilenameFilter, int)
had gotten the content of path/** /sub twice.
- 2013-03-29 Hartmut new:
cleandir(File)
- 2013-02-13 Hartmut chg:
addFileToList(String, AddFileToList)
new better algorithm
- 2013-02-03 Hartmut chg: the
addFileToList(String, AddFileToList)
does not throw a FileNotFoundException
instead it returns false. All try-catch Blocks of a user calling environment may be changed to catch(Exception e)
instead of catch(FileNotFoundException e)
as simple work arround.
- 2013-02-03 Hartmut new:
#normalizePath(String)
- 2013-02-03 Hartmut chg:
addFilesWithBasePath(File, String, List)
improved
- 2013-01-20 Hartmut bugfix:
addFilesWithBasePath(File, String, List)
:If a /../ is used in the path,
it was faulty. Usage of canonicalpath instead absolute path.
addFileToList(File, String, AddFileToList)
: Sometimes dir not regarded.
- 2013-01-12 Hartmut new: Method checkNewless(src, dst, deleteIt)
- 2012-12-30 Hartmut chg:
addFilesWithBasePath(File, String, List)
now gets a base directory.
- 2012-12-25 Hartmut chg:
mkDirPath(String)
now returns the directory which is designated by the argument
and checks whether it is a directory, not a file. It is more stronger, elsewhere compatible.
- 2012-08-12 Hartmut chg:
addFileToList(File, String, List)
some changes to support zipfiles.
- 2012-01-26 Hartmut new:
isSymbolicLink(File)
and cleanAbsolutePath(String)
.
- 2012-01-05 Hartmut new:
rmdir(File)
- 2011-08-13 Hartmut chg:
#addFilesWithBasePath(String, List)
now stores the localPath
with '/' instead backslash on windows too. Strategy: Use slash generally in Java-applications.
Only a java.lang.File instance can contain backslash, because it is gotten from basic file routines
such as File.listFiles() called in addFileToList(..). TODO use File.list() instead File.listFiles()
and build the File-instance after replace('/', '\\'). The advantage of only have slash: The user
should not be search to both backslash and slash while evaluating a file path.
In Java a path with slash works proper any time.
Only if a line for execution of the windows operation systems command shell is generated,
all slash have to be converted to backslash lastly. See change 2011-06-22 of this file:
getCanonicalPath(File)
returns slash in operation system MS-Windows too.
- 2011-07-10 Hartmut new:
absolutePath(String, File)
. The requirement was:
Usage of "~/path" to select in the users home in linux.
- 2011-06-22
getCanonicalPath(File)
returns slash in operation system MS-Windows too.
- 2011-07-10 Hartmut
- 2009-12-29 Hartmut bugfix: addFilesWithBasePath(...): If the sPath contains a ':' on second pos (Windows Drive), than the method hadn't accepted a ':' inside, fixed.
- 2009-12-29 Hartmut bugfix: mkDirPath(...): not it accepts '\' too (Windows)
- 2009-12-29 Hartmut corr: addFileToList(...): uses now currentDir, if param dir== null.
- 2009-12-29 Hartmut new: method relativatePath(String sInput, String sRefDir)
- 2009-05-06 Hartmut new: writeFile(content, file);
- 2009-05-06 Hartmut bugfix: addFileToList(): A directory was also added like a file if wildcards are used. It is false, now filtered file.isFile().
- 2009-03-24 Hartmut new: isAbsolutePathOrDrive()
- 2008-04-02 Hartmut corr: some changes
- 2007-10-15 Hartmut: creation
- 2007 Hartmut: created
public static boolean addFilesWithBasePath(java.io.File baseDir, java.lang.String sPathArg, java.util.List<FileFunctions.FileAndBasePath> list)
addFilesWithBasePath("..\\example/dir:localdir/ ** /*.h", list);fills
../example/dir/
in all elements basePath of list,
and fills all files with mask *.h
from localdir and all sub folders,
with the local name part starting with localdir/...
in all elements localPath.baseDir
- A base directoy which is the base of sPath, or which is the current directory to resolve relative
to absolute paths. It can be null, then sPath should describe a valid
file path, if relative then starting from the system's current directory new File(".").getAbsolutePath
).sPathArg
- may contain a :
on position >2 instead of /
to separate the base path from a local path (@since 2012, old feature).
list
- The list to fill in files with the basepath.
The basepath is ""
if no basepath is given in sPath.java.io.FileNotFoundException
public static boolean setCurrdirFromFile(java.lang.String file)
+setWDtoTmp.bat
or +setWDtoTmp.sh
file
- The file path can contain $(ENV) which will be expanded.System.setProperty(String, String)
is executed with setProperty("user.dir", ... file content...)
Check outside whether the currdir is correct.public static java.lang.String readFile(java.io.File file)
file
- The file should be exist, but don't need to exist.public static java.lang.String readFile(java.io.File file, java.lang.Appendable dst, int[] zdst)
file
- The file should be exist, but don't need to exist.dst
- any appendable, may be an opened other file too, for copypublic static java.lang.CharSequence readInJar(java.lang.Class clazz, java.lang.String pathInJar, java.lang.String encoding)
clazz
- The path starts in the package where this class is located and uses the ClassLoader of this class.pathInJar
- relative to the class.
Use "filename.ext" if the file is in the same package as the clazz.
Use "../package/filename.ext" if the file is in another parallel package.
Note: The file is searched in the bin tree of class files on using in Eclipse IDEencoding
- use usually "UTF-8" or "US-ASCII" or "ISO-8859-1" for byte-coded content
or "UTF-16BE"public static boolean writeFile(java.lang.String content, java.lang.String sFile)
content
- any textual informationsFile
- The path of the file.public static boolean writeFile(java.lang.String content, java.io.File file)
content
- any textual informationsFile
- The file.public static byte[] readBinFile(java.io.File file)
file
- The file should be exist, but don't need to exist.public static int readBinFile(java.io.File file, byte[] buffer)
FileInputStream
, read and closed again.file
- The file should be exist, but don't need to exist.public static int writeBinFile(java.io.File file, byte[] buffer)
file
- The file should be exist, but don't need to exist.public static int copyFile(java.io.File src, java.io.File dst) throws java.io.IOException
src
- A src file.dst
- The dst directory should be exist. Use mkDirPath(String)
with this dst to create it.java.io.IOException
- Any error. but not if the src file not found.public static int copyFile(java.io.File src, java.io.File dst, boolean bKeepTimestamp, boolean bOverwrite, boolean bOverwriteReadonly) throws java.io.IOException
src
- A src file.dst
- The dst directory should be exist. Use mkDirPath(String)
with this dst to create it.bKeepTimestamp
- true then sets the time stamp of the dst to the time stamp of the src.bOverwrite
- if true then overwrite an existing file. If false exception on existing file.bOverwriteReadonly
- if true then overwrite a read-only file if necessary.
If false exception on trying overwrite a read-only file.java.io.IOException
- Any error. but not if the src file not found.java.lang.IllegalArgumentException
- if the dst cannot be overwritten.public static void mkDirPath(java.io.File file) throws java.io.FileNotFoundException
mkDirPath(String)
with "/" on end to create a directory.file
- Either any file or any directory with given path.java.io.IOException
- If the path is not makeable.java.io.FileNotFoundException
public static java.io.File mkDirPath(java.lang.String sPath) throws java.io.FileNotFoundException
sPath
- The path to a file or directory if it ends with "/" or "\".
It means, a file name on end will ignored. It makes the directory for the file.
The used path to a directory is all before the last / or backslash.java.io.FileNotFoundException
- If the path exists and it is a file or if it is not able to create.public static java.io.File mkDir(java.io.File dir) throws java.io.FileNotFoundException
sPath
- The path to a directory.java.io.FileNotFoundException
- If the path exists and it is a file or if it is not able to create.public static boolean delete(java.lang.String path)
public static boolean rmdir(java.io.File dir)
dir
- A directory or a file inside the directorypublic static boolean cleandir(java.io.File dir_file)
dir_file
- A directory or any file inside the directory.public static boolean cleandirForced(java.io.File dir_file)
cleandir(File)
, the last one aborts if one file cannot delete.dir_file
- A directory or any file inside the directory.public static boolean copyDir(java.io.File src, java.io.File dst)
cleandir(File)
, the last one aborts if one file cannot delete.dir_file
- A directory or any file inside the directory.public static java.io.File getDirectory(java.io.File file) throws java.io.FileNotFoundException
File.getParentFile()
does not return the directory if the File is described as a relative path
which does not contain a directory. This method builds the absolute path of the input file and returns its directory.file
- java.io.FileNotFoundException
public static java.io.File[] getFiles(java.io.File dir, java.lang.CharSequence nameWithWildcard)
java.io.File-listFiles
but builds a filter internally.dir
- nameWithWildcard
- Can have up to 2 '*' as wildcardspublic static java.io.File getFirstFileWildcard(java.io.File file)
dir
- nameWithWildcard
- Can have up to 2 '*' as wildcardspublic static java.io.File getDir(java.io.File file)
File.getParentFile()
does not return the directory if the File is described as a relative path
which does not contain a directory. This method builds the absolute path of the input file and returns its directory.file
- public static boolean isAbsolutePath(java.lang.CharSequence filePath)
filePath
- public static boolean isAbsolutePathOrDrive(java.lang.CharSequence filePath)
filePath
- public static java.lang.String getCanonicalPath(java.io.File file)
normalizePath(CharSequence)
or normalizePath(File)
.file
- The given filepublic static java.lang.String relativatePath(java.lang.String sInput, java.lang.String sRefPath)
a/b/c/d | a/b | c/d | If the sRefDirectory is within sInput, sInput will be shortened: |
a/b/c/d | x/y | ../../a/b/c/d | If the sRefDirectory is parallel to sInput, ../ will be added and sRefDirectory will be added: |
a/b/c/d | a/x | ../b/c/d | If the sRefDirectory is within sInput but parallel, ../ will be added and the rest of sInput will be shortened: |