public final class UtilFiles extends Object
Modifier and Type | Method and Description |
---|---|
static Path |
createPathInExistingRootFolder(String rootPath,
String childFilePath)
Create a child file path from a given root with its parent folder recursively,
but do not attempt to create the first provided root folder.
|
static byte[] |
createTar(String dirOrFilePath)
Create a tar archive in memory and return the corresponding byte array.
|
static byte[] |
createTar(String root,
List<Path> files) |
static void |
deleteIfExists(File file)
This is interesting to make your code shorter while compliant with sonar.
|
static String |
getFileExtension(String filename)
Return file extension from name or path
|
static String |
getPerDayPath(long ms)
Given q long return a file path with year month day.
|
static String |
getPerDayPath(ZonedDateTime zdt)
Given a
ZonedDateTime returns a suffix like "2019/11/09". |
static boolean |
isExistingDir(String dir)
Check if a local unix filesystem directory exists
|
static List<String> |
listDirectoriesInADirectory(String dirPath)
List of the files in a directory and his subdirectories
|
static void |
listFiles(Path path,
List<Path> files,
boolean withHidden,
boolean withSymbolicLink)
List of the files in a directory and his subdirectories (without suffix)
|
static void |
listFiles(Path path,
List<Path> files,
String suffix,
boolean withHidden,
boolean withSymbolicLink)
List of the files in a directory and his subdirectories
|
static List<String> |
listFilesInADirectory(String dirPath)
List of the files in a directory
|
static String |
readHjson(String path) |
static void |
recursiveDelete(Path rootPathToDelete)
Recursively delete all directories and files contained
in a specified path.
|
static int |
writeToFile(String dirPath,
String absFileName,
ByteBuffer data)
Write some byte buffer to a file.
|
public static void listFiles(Path path, List<Path> files, boolean withHidden, boolean withSymbolicLink) throws IOException
path
- the pathfiles
- found fileswithHidden
- include hidden fileswithSymbolicLink
- include symbolic linksIOException
- if some of the file cannot be readpublic static void listFiles(Path path, List<Path> files, String suffix, boolean withHidden, boolean withSymbolicLink) throws IOException
path
- directory pathfiles
- list of filessuffix
- the suffix to searchwithHidden
- include or not hidden fileswithSymbolicLink
- include or not symbolic linksIOException
- if some of the file cannot be readpublic static List<String> listDirectoriesInADirectory(String dirPath)
dirPath
- directory pathpublic static List<String> listFilesInADirectory(String dirPath)
dirPath
- directory pathpublic static byte[] createTar(String dirOrFilePath) throws IOException
dirOrFilePath
- directory or file pathIOException
- io exceptionpublic static byte[] createTar(String root, List<Path> files) throws IOException
IOException
public static void recursiveDelete(Path rootPathToDelete) throws IOException
Note: deletion is not really recursive (method does not call itself), but its uses Files.walk() method, which recursively all directories and files contained in a directory. Next it's important to reverse list order to delete deep files first.
If a file is created during the whole deletion, an IOException will be thrown.
rootPathToDelete
- root path containing directories and files to deleteIOException
- if some of the file could not be deletedpublic static String getPerDayPath(long ms)
Example : UtilFiles.getPath(1573272040575L) returns "2019/11/09".
ms
- a long timestamppublic static String getPerDayPath(ZonedDateTime zdt)
ZonedDateTime
returns a suffix like "2019/11/09".zdt
- a ZonedDateTime
public static int writeToFile(String dirPath, String absFileName, ByteBuffer data) throws IOException
dirPath
- Path to target directoryabsFileName
- Full filenamedata
- Bytes to writeIOException
- : Error when writingpublic static boolean isExistingDir(String dir)
dir
- the absolute directory path* @throws IOException if notpublic static Path createPathInExistingRootFolder(String rootPath, String childFilePath) throws IOException
rootPath
- the root absolute pathchildFilePath
- the child path.IOException
- if the root does not exists or one of the intermediary path is a
existing filepublic static void deleteIfExists(File file)
file
- a file handlepublic static String getFileExtension(String filename)
filename
- file name or path (eg. toto.md, /tmp/test/my_file.txt)public static String readHjson(String path) throws IOException
path
- absolute path of a file whose content is either json or hjson validIOException
Copyright © 2014–2022. All rights reserved.