Class FileUtilities
- java.lang.Object
-
- org.hortonmachine.gears.utils.files.FileUtilities
-
public class FileUtilities extends Object
Various file utilities useful when dealing with bytes, bits and numbers
- Since:
- 1.1.0
- Author:
- Andrea Antonello - www.hydrologis.com
-
-
Constructor Summary
Constructors Constructor Description FileUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopyFile(File in, File out)static voidcopyFile(String fromFile, String toFile)static booleandeleteFileOrDir(File filehandle)Returns true if all deletions were successful.static voidfolderCheckMakeOrDie(String path)Checks a path to be a folder.static longgetCreationTimestamp(String path)static File[]getFilesListByExtention(String folderPath, String ext)Get the list of files in a folder by its extension.static StringgetNameWithoutExtention(File file)Returns the name of the file without the extension.static StringgetSafeFileName(String fileName)Makes a file name safe to be used.static StringreadFile(File file)Read text from a file in one line.static StringreadFile(String filePath)Read text from a file in one line.static byte[]readFileToBytes(String filePath)Read a file into a byte array.static LinkedHashMap<String,String>readFileToHashMap(String filePath, String separator, boolean valueFirst)Method to read a properties file into aLinkedHashMap.static List<String>readFileToLinesList(File file)Read text from a file to a list of lines.static List<String>readFileToLinesList(String filePath)Read text from a file to a list of lines.static StringreadInputStreamToString(InputStream inputStream)Read from an inoutstream and convert the readed stuff to a String.static StringreplaceBackSlashes(String path)static StringreplaceBackSlashesWithSlashes(String string)Replaces backslashes with /.static FilestringAsTmpFile(String string)"Converts" a string to a temporary file.static FilestringListAsTmpFile(List<String> list)"Converts" a List of strings to a temporary file.static FilesubstituteExtention(File file, String newExtention)Substitute the extention of a file.static voidwriteFile(String text, File file)Write text to a file in one line.static voidwriteFile(List<String> lines, File file)Write a list of lines to a file.static voidwriteFile(List<String> lines, String filePath)Write a list of lines to a file.
-
-
-
Method Detail
-
copyFile
public static void copyFile(String fromFile, String toFile) throws IOException
- Throws:
IOException
-
copyFile
public static void copyFile(File in, File out) throws IOException
- Throws:
IOException
-
getCreationTimestamp
public static long getCreationTimestamp(String path) throws IOException
- Throws:
IOException
-
deleteFileOrDir
public static boolean deleteFileOrDir(File filehandle) throws IOException
Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.- Parameters:
filehandle- the file or folder to remove.- Returns:
- true if all deletions were successful
- Throws:
IOException
-
readFileToBytes
public static byte[] readFileToBytes(String filePath) throws IOException
Read a file into a byte array.- Parameters:
filePath- the path to the file.- Returns:
- the array of bytes.
- Throws:
IOException
-
readInputStreamToString
public static String readInputStreamToString(InputStream inputStream) throws IOException
Read from an inoutstream and convert the readed stuff to a String. Usefull for text files that are available as streams.- Parameters:
inputStream-- Returns:
- the read string
- Throws:
IOException
-
readFile
public static String readFile(String filePath) throws IOException
Read text from a file in one line.- Parameters:
filePath- the path to the file to read.- Returns:
- the read string.
- Throws:
IOException
-
readFile
public static String readFile(File file) throws IOException
Read text from a file in one line.- Parameters:
file- the file to read.- Returns:
- the read string.
- Throws:
IOException
-
readFileToLinesList
public static List<String> readFileToLinesList(String filePath) throws IOException
Read text from a file to a list of lines.- Parameters:
filePath- the path to the file to read.- Returns:
- the list of lines.
- Throws:
IOException
-
readFileToLinesList
public static List<String> readFileToLinesList(File file) throws IOException
Read text from a file to a list of lines.- Parameters:
file- the file to read.- Returns:
- the list of lines.
- Throws:
IOException
-
writeFile
public static void writeFile(String text, File file) throws IOException
Write text to a file in one line.- Parameters:
text- the text to write.file- the file to write to.- Throws:
IOException
-
writeFile
public static void writeFile(List<String> lines, String filePath) throws IOException
Write a list of lines to a file.- Parameters:
lines- the list of lines to write.filePath- the path to the file to write to.- Throws:
IOException
-
writeFile
public static void writeFile(List<String> lines, File file) throws IOException
Write a list of lines to a file.- Parameters:
lines- the list of lines to write.file- the file to write to.- Throws:
IOException
-
replaceBackSlashesWithSlashes
public static String replaceBackSlashesWithSlashes(String string)
Replaces backslashes with /.- Parameters:
string- the string to check.- Returns:
- the string without backslashes.
-
getNameWithoutExtention
public static String getNameWithoutExtention(File file)
Returns the name of the file without the extension.Note that if the file has no extension, the name is returned.
- Parameters:
file- the file to trim.- Returns:
- the name without extension.
-
substituteExtention
public static File substituteExtention(File file, String newExtention)
Substitute the extention of a file.- Parameters:
file- the file.newExtention- the new extention (without the dot).- Returns:
- the file with the new extention.
-
getSafeFileName
public static String getSafeFileName(String fileName)
Makes a file name safe to be used.- Parameters:
fileName- the file name to "encode".- Returns:
- the safe filename.
-
readFileToHashMap
public static LinkedHashMap<String,String> readFileToHashMap(String filePath, String separator, boolean valueFirst) throws IOException
Method to read a properties file into aLinkedHashMap.Empty lines are ignored, as well as lines that do not contain the separator.
- Parameters:
filePath- the path to the file to read.separator- the separator ornull. Defaults to '='.valueFirst- iftrue, the second part of the string is used as key.- Returns:
- the read map.
- Throws:
IOException
-
stringAsTmpFile
public static File stringAsTmpFile(String string) throws Exception
"Converts" a string to a temporary file.Useful for those modules that want a file in input and one wants to use the parameters.
- Parameters:
string- the string to write to the file.- Returns:
- the created file.
- Throws:
Exception
-
stringListAsTmpFile
public static File stringListAsTmpFile(List<String> list) throws Exception
"Converts" a List of strings to a temporary file.Useful for those modules that want a file in input and one wants to use the parameters.
- Parameters:
list- the list of strings to write to the file (one per row).- Returns:
- the created file.
- Throws:
Exception
-
getFilesListByExtention
public static File[] getFilesListByExtention(String folderPath, String ext)
Get the list of files in a folder by its extension.- Parameters:
folderPath- the folder path.ext- the extension without the dot.- Returns:
- the list of files patching.
-
folderCheckMakeOrDie
public static void folderCheckMakeOrDie(String path) throws ModelsIOException
Checks a path to be a folder. If it doesn't exist, it tries to create it.- Throws:
ModelsIOException- if the path is null or impossible to create.
-
-