Package com.tagtraum.core
Class FileUtilities
- java.lang.Object
-
- com.tagtraum.core.FileUtilities
-
public final class FileUtilities extends Object
File utilities.- Author:
- Hendrik Schreiber
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
canWrite(Path path)
Checks whether a file can be written or not.static void
copy(InputStream in, Path to)
Copy a stream to a file, replacing the file if it exists.static void
copy(Path[] orig, Path[] copies)
Copy an array of files to a new array of files.static void
copy(Path from, Path to)
Copy a file, preserving its attributes, replacing the target file, if it exists.static void
copyFiles(Path fromDir, Path toDir)
Copy all regular files (i.e.static String
createValidFilename(String name)
Creates a name that only contains letters, digits, spaces and underscores.static void
delete(Path... files)
Delete the files in the given array.static void
deleteRecursively(Path path)
Deletes a directory recursively, thus allowing its deletion.static String
getExtension(String filename)
Get extension (with the dot).static String
getExtension(Path file)
Get extension (with the dot).static String
getExtensionWithoutDot(String filename)
Get extension (without the dot).static String
getExtensionWithoutDot(Path file)
Get extension (without the dot).static Path
getFreeFile(Path file)
Attempt to find a free file name, based on a given filename, while preserving the extension.static Path
getVolume(Path file)
Extracts the volume/drive part from a file.static String
getVolumeName(Path file)
Volume name.static String
injectBeforeExtension(String filename, String injection)
Inject an arbitrary string before the extension.static boolean
isVolumeAvailable(Path file)
Indicates whether a drive or volume that a file is in is mounted.static boolean
notExists(Path file, long maxAgeInMS)
Tests whether the given file does not exist.static String
removeExtension(String filename)
Remove file extension, i.e.
-
-
-
Method Detail
-
canWrite
public static boolean canWrite(Path path) throws NoSuchFileException
Checks whether a file can be written or not.- Parameters:
path
- path- Returns:
- true, if we can write
- Throws:
NoSuchFileException
-
notExists
public static boolean notExists(Path file, long maxAgeInMS)
Tests whether the given file does not exist. Results of the test are cached to avoid accessing the file system or network too often.- Parameters:
file
- filemaxAgeInMS
- max age of the returned result in milliseconds- Returns:
true
if we know for sure that the file didn't exist up tomaxAgeInMS
ago- See Also:
Files.notExists(Path, LinkOption...)
,DirectoryFileExistence
-
getExtension
public static String getExtension(Path file)
Get extension (with the dot).- Parameters:
file
- file- Returns:
- extension (with '.') or empty string.
-
getExtensionWithoutDot
public static String getExtensionWithoutDot(Path file)
Get extension (without the dot).- Parameters:
file
- file- Returns:
- extension (without '.') or empty string.
-
getExtension
public static String getExtension(String filename)
Get extension (with the dot).- Parameters:
filename
- filename- Returns:
- extension (with '.') or empty string.
-
getExtensionWithoutDot
public static String getExtensionWithoutDot(String filename)
Get extension (without the dot).- Parameters:
filename
- filename- Returns:
- extension (without '.') or empty string.
-
removeExtension
public static String removeExtension(String filename)
Remove file extension, i.e. everything after the last dot, including the dot.- Parameters:
filename
- filename- Returns:
- filename without extension
-
injectBeforeExtension
public static String injectBeforeExtension(String filename, String injection)
Inject an arbitrary string before the extension. Useful for transforming image filename likeimage.png
to[email protected]
- Parameters:
filename
- filenameinjection
- injection, arbitrary string- Returns:
- modified file name
-
createValidFilename
public static String createValidFilename(String name)
Creates a name that only contains letters, digits, spaces and underscores.- Parameters:
name
- name- Returns:
- valid name
-
copy
public static void copy(InputStream in, Path to) throws IOException
Copy a stream to a file, replacing the file if it exists. Resource management of the inputstream is left to the caller.
Equivalent with:
Files.copy(in, to, StandardCopyOption.REPLACE_EXISTING)
- Parameters:
in
- input streamto
- file to write to- Throws:
IOException
- if something goes wrong
-
copy
public static void copy(Path from, Path to) throws IOException
Copy a file, preserving its attributes, replacing the target file, if it exists.Equivalent with:
Files.copy(from, to, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING)
- Parameters:
from
- file to copyto
- file to copy to- Throws:
IOException
- if something goes wrong
-
copyFiles
public static void copyFiles(Path fromDir, Path toDir) throws IOException
Copy all regular files (i.e. no directories) from one directory to another.- Parameters:
fromDir
- from dirtoDir
- to dir- Throws:
IOException
- exception
-
isVolumeAvailable
public static boolean isVolumeAvailable(Path file)
Indicates whether a drive or volume that a file is in is mounted.- Parameters:
file
- file to check- Returns:
- true or false
-
getVolume
public static Path getVolume(Path file)
Extracts the volume/drive part from a file.- Parameters:
file
- file- Returns:
- volume or drive or
null
, if none could be discovered
-
getVolumeName
public static String getVolumeName(Path file)
Volume name.- Parameters:
file
- file- Returns:
- volume or
null
, if none available
-
deleteRecursively
public static void deleteRecursively(Path path) throws IOException
Deletes a directory recursively, thus allowing its deletion.- Parameters:
path
- path- Throws:
IOException
- if deletion fails
-
delete
public static void delete(Path... files)
Delete the files in the given array.- Parameters:
files
- files
-
copy
public static void copy(Path[] orig, Path[] copies) throws IOException
Copy an array of files to a new array of files. If a copy file name is empty, a temp file with the same extension as the original is created automatically.- Parameters:
orig
- originalscopies
- copies- Throws:
IOException
- if something goes wrong
-
getFreeFile
public static Path getFreeFile(Path file) throws IOException
Attempt to find a free file name, based on a given filename, while preserving the extension.- Parameters:
file
- file- Returns:
- free file
- Throws:
IOException
- if we fail.
-
-