Package org.goplanit.utils.misc
Class FileUtils
- java.lang.Object
-
- org.goplanit.utils.misc.FileUtils
-
public class FileUtils extends Object
Lightweight File utilities- Author:
- markr
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
callForEachFileIn(String pathToDir, Consumer<File> callBack)
Call the callback for each file in the directory providedstatic File
create(URL url)
Create from URL given it is a local filestatic boolean
deleteDirectory(File directoryToDelete)
Delete a directory by providing a file that represents a directory.static String
getExtension(File file)
Collect the extension of a file.static InputSource
getFileContentAsInputSource(String filePath, String charSetEncoding)
Given a location, construct input sourcestatic String
getFileNameWithoutExtension(String fileName)
Extract the file name without the extension, i.e.static File[]
getFilesWithExtensionFromDir(String pathToDir, String fileExtension)
Collect all files from a directory with the given extensionstatic String
parseFileContentAsString(String filePath, String charSetEncoding)
Construct a string based on the file contentsstatic String
parseUtf8FileContentAsString(String filePath)
Construct a string based on the file contents in UTF8 character encouding
-
-
-
Field Detail
-
DOT
public static final String DOT
- See Also:
- Constant Field Values
-
ZIP
public static final String ZIP
- See Also:
- Constant Field Values
-
-
Method Detail
-
getExtension
public static String getExtension(File file)
Collect the extension of a file. In case the file is hidden (starts with "." it does not count as extension)- Parameters:
file
- the file to get it from- Returns:
- the extension, if it does not exist return empty string
-
getFilesWithExtensionFromDir
public static File[] getFilesWithExtensionFromDir(String pathToDir, String fileExtension)
Collect all files from a directory with the given extension- Parameters:
pathToDir
- path to dirfileExtension
- the file extension, e.g. ".xml"- Returns:
- the list of files that match this extension in the dir
-
getFileNameWithoutExtension
public static String getFileNameWithoutExtension(String fileName)
Extract the file name without the extension, i.e. remove the last part of the string after the last "." encountered- Parameters:
fileName
- to use- Returns:
- fileName without extension
-
callForEachFileIn
public static void callForEachFileIn(String pathToDir, Consumer<File> callBack)
Call the callback for each file in the directory provided- Parameters:
pathToDir
- to checkcallBack
- to use no each file found in dir
-
create
public static File create(URL url)
Create from URL given it is a local file- Parameters:
url
- to use- Returns:
- file
-
deleteDirectory
public static boolean deleteDirectory(File directoryToDelete)
Delete a directory by providing a file that represents a directory. In which case we recursively delete all files in the directory and then the directory itself.- Parameters:
directoryToDelete
- the directory to delete- Returns:
- success of deletion
-
getFileContentAsInputSource
public static InputSource getFileContentAsInputSource(String filePath, String charSetEncoding) throws FileNotFoundException, UnsupportedEncodingException
Given a location, construct input source- Parameters:
filePath
- to usecharSetEncoding
- to apply- Returns:
- created input source
- Throws:
FileNotFoundException
- thrown if errorUnsupportedEncodingException
- thrown if error
-
parseFileContentAsString
public static String parseFileContentAsString(String filePath, String charSetEncoding) throws IOException
Construct a string based on the file contents- Parameters:
filePath
- to usecharSetEncoding
- to apply- Returns:
- created string representation
- Throws:
IOException
- thrown if error
-
parseUtf8FileContentAsString
public static String parseUtf8FileContentAsString(String filePath) throws IOException
Construct a string based on the file contents in UTF8 character encouding- Parameters:
filePath
- to use- Returns:
- created string representation
- Throws:
IOException
- thrown if error
-
-