Package org.apache.wicket.util.file
Class Files
- java.lang.Object
-
- org.apache.wicket.util.file.Files
-
public class Files extends java.lang.Object
File utility methods.- Author:
- Jonathan Locke
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
basePath(java.lang.String path, java.lang.String extension)
Strips off the given extension (probably returned from Files.extension()) from the path, yielding a base pathname.static java.lang.String
cleanupFilename(java.lang.String filename)
Replaces commonly unsupported characters with '_'static void
copy(java.io.File sourceFile, java.io.File targetFile)
make a copy of a filestatic java.lang.String
extension(java.lang.String path)
Gets extension from pathstatic java.lang.String
filename(java.lang.String path)
Gets filename from pathstatic java.time.Instant
getLastModified(java.io.File file)
get last modification timestamp for filestatic java.io.File
getLocalFileFromUrl(java.lang.String url)
for urls that point to local files (e.g.static java.io.File
getLocalFileFromUrl(java.net.URL url)
for urls that point to local files (e.g.static java.io.File[]
list(java.io.File file)
List all files inside the given file.static boolean
mkdirs(java.io.File folder)
Utility method for creating a directory.static byte[]
readBytes(java.io.File file)
read binary file fullystatic boolean
remove(java.io.File file)
Deletes a normal file.static boolean
removeAsync(java.io.File file, IFileCleaner fileCleaner)
Schedules a file for removal asynchronously.static boolean
removeFolder(java.io.File folder)
Deletes a folder by recursively removing the files and folders inside it.static boolean
removeFolderAsync(java.io.File folder, IFileCleaner fileCleaner)
Schedules a folder and all files inside it for asynchronous removal.static int
writeTo(java.io.File file, java.io.InputStream input)
Writes the given input stream to the given filestatic int
writeTo(java.io.File file, java.io.InputStream input, int bufSize)
Writes the given input stream to the given file
-
-
-
Method Detail
-
basePath
public static java.lang.String basePath(java.lang.String path, java.lang.String extension)
Strips off the given extension (probably returned from Files.extension()) from the path, yielding a base pathname.- Parameters:
path
- The path, possibly with an extension to stripextension
- The extension to strip, or null if no extension exists- Returns:
- The path without any extension
-
extension
public static java.lang.String extension(java.lang.String path)
Gets extension from path- Parameters:
path
- The path- Returns:
- The extension, like "bmp" or "html", or null if none can be found
-
filename
public static java.lang.String filename(java.lang.String path)
Gets filename from path- Parameters:
path
- The path- Returns:
- The filename
-
remove
public static boolean remove(java.io.File file)
Deletes a normal file.If the file cannot be deleted for any reason then at most 50 retries are attempted with delay of 100ms at each 10th attempt.
- Parameters:
file
- the file to delete- Returns:
true
if file was deleted,false
if the file don't exist, is a folder or cannot be removed for some reason
-
removeFolder
public static boolean removeFolder(java.io.File folder)
Deletes a folder by recursively removing the files and folders inside it. Delegates the work toremove(File)
for plain files.- Parameters:
folder
- the folder to delete- Returns:
true
if the folder is deleted successfully.
-
removeAsync
public static boolean removeAsync(java.io.File file, IFileCleaner fileCleaner)
Schedules a file for removal asynchronously.- Parameters:
file
- the file to be removedfileCleaner
- the file cleaner that will be used to remove the file- Returns:
false
if thefile
is null or a folder,true
- otherwise (i.e. if it is scheduled)
-
removeFolderAsync
public static boolean removeFolderAsync(java.io.File folder, IFileCleaner fileCleaner)
Schedules a folder and all files inside it for asynchronous removal.- Parameters:
folder
- the folder to be removedfileCleaner
- the file cleaner that will be used to remove the file- Returns:
false
if thefolder
is null or a normal file,true
- otherwise (i.e. if it is scheduled)
-
writeTo
public static int writeTo(java.io.File file, java.io.InputStream input) throws java.io.IOException
Writes the given input stream to the given file- Parameters:
file
- The file to write toinput
- The input- Returns:
- Number of bytes written
- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.io.File file) throws java.io.IOException
read binary file fully- Parameters:
file
- file to read- Returns:
- byte array representing the content of the file
- Throws:
java.io.IOException
- is something went wrong
-
writeTo
public static int writeTo(java.io.File file, java.io.InputStream input, int bufSize) throws java.io.IOException
Writes the given input stream to the given file- Parameters:
file
- The file to write toinput
- The inputbufSize
- The memory buffer size. 4096 is a good value.- Returns:
- Number of bytes written
- Throws:
java.io.IOException
-
cleanupFilename
public static java.lang.String cleanupFilename(java.lang.String filename)
Replaces commonly unsupported characters with '_'
- Parameters:
filename
- to be cleaned- Returns:
- cleaned filename
-
copy
public static void copy(java.io.File sourceFile, java.io.File targetFile) throws java.io.IOException
make a copy of a file- Parameters:
sourceFile
- source file that needs to be clonedtargetFile
- target file that should be a duplicate of source file- Throws:
java.io.IOException
- if something went wrong
-
getLocalFileFromUrl
public static java.io.File getLocalFileFromUrl(java.net.URL url)
for urls that point to local files (e.g. 'file:' or 'jar:file:') this methods returns a reference to the local file- Parameters:
url
- url of the resource- Returns:
- reference to a local file if url contains one,
null
otherwise - See Also:
getLocalFileFromUrl(String)
-
getLocalFileFromUrl
public static java.io.File getLocalFileFromUrl(java.lang.String url)
for urls that point to local files (e.g. 'file:' or 'jar:file:') this methods returns a reference to the local file- Parameters:
url
- url of the resource- Returns:
- reference to a local file if url contains one,
null
otherwise - See Also:
getLocalFileFromUrl(URL)
-
getLastModified
public static java.time.Instant getLastModified(java.io.File file)
get last modification timestamp for file- Parameters:
file
-- Returns:
- timestamp
-
mkdirs
public static boolean mkdirs(java.io.File folder)
Utility method for creating a directory. If the creation didn't succeed for some reason then at most 50 attempts are made with delay of 100ms at every 10th attempt.- Parameters:
folder
- the folder to create- Returns:
true
if the creation is successful,false
- otherwise
-
list
public static java.io.File[] list(java.io.File file)
List all files inside the given file.- Parameters:
file
- directory- Returns:
- files, never
null
-
-