Package org.eclipse.epsilon.common.util
Class FileUtil
- java.lang.Object
-
- org.eclipse.epsilon.common.util.FileUtil
-
public class FileUtil extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkFileExists(File file)
static void
copy(File srcFile, File dstFile)
static File
copyToTemp(File srcFile)
static File
createTempDir(String name)
static File
createTempDir(String name, boolean reuse)
static File
createTempFile(String name)
static File
createTempFile(String name, String extension)
static void
deleteDirectory(String dir)
static void
deleteDirectory(Path path)
WARNIING: Use with caution! Deletes all contents and sub-directories of the specified path.static String
getAbsolutePath(String basePath, String relativePath)
static Path
getCurrentDirectory()
static File
getDirectoryOf(Class<?> clazz)
static File
getDirectoryStandalone(String dir, Class<?> relativeTo)
Convenience method for copying all files from the workspace / JAR path (relative to the class) to temp folder.static String
getExtension(String filename)
Copied from @linkplain{https://stackoverflow.com/a/3571239/5870336}static String
getFileContents(File file)
static Collection<String>
getFileLineContents(File file)
static String
getFileName(String path)
static String
getFileName(String path, boolean includeExtension)
static File
getFileStandalone(String name, Class<?> relativeTo)
Gets a file stored as a resource in a jar.static File
getFileURL(String name, Class<?> relativeTo)
The getFile method of old, before we tried to fix it to work in JAR files.static String
getPath(String name, Class<?> relativeTo)
static Path
getStandalonePath(String dir, Class<?> relativeTo)
static boolean
isInJarFile()
static boolean
isInJarFile(Class<?> clazz)
static Set<String>
listFilesAsSet(File fileExpected)
static Map<Path,byte[]>
readDirectory(String dir)
static Map<Path,byte[]>
readDirectory(Path dir)
Reads entire directory recursively, mapping the contents of each file as a string to its path.static String
removeExtension(String filename)
static String
replaceExtension(String filename, String newExtension)
static boolean
sameContents(File fileExpected, File fileActual, Set<String> ignoreFilenames)
We implement our own comparison algorithm here, so we don't need Eclipse Compare to compute differences, but rather only to show them in the UI.static boolean
sameContents(InputStream isExpected, InputStream isActual)
static void
setFileContents(String str, File file)
static Path
stringToPath(String dir)
-
-
-
Method Detail
-
getCurrentDirectory
public static Path getCurrentDirectory()
-
setFileContents
public static void setFileContents(String str, File file) throws Exception
- Throws:
Exception
-
getExtension
public static String getExtension(String filename)
Copied from @linkplain{https://stackoverflow.com/a/3571239/5870336}- Parameters:
filename
-- Returns:
- Since:
- 1.6
-
getFileLineContents
public static Collection<String> getFileLineContents(File file) throws Exception
- Throws:
Exception
-
isInJarFile
public static boolean isInJarFile()
- Returns:
- Since:
- 1.6
-
isInJarFile
public static boolean isInJarFile(Class<?> clazz)
- Parameters:
clazz
-- Returns:
- Since:
- 1.6
-
getStandalonePath
public static Path getStandalonePath(String dir, Class<?> relativeTo) throws IOException
- Throws:
IOException
-
getDirectoryStandalone
public static File getDirectoryStandalone(String dir, Class<?> relativeTo) throws IOException
Convenience method for copying all files from the workspace / JAR path (relative to the class) to temp folder. Used for tests.- Parameters:
dir
-relativeTo
-- Returns:
- Throws:
IOException
URISyntaxException
- Since:
- 1.6
- See Also:
getFileStandalone(String, Class)
-
getFileStandalone
public static File getFileStandalone(String name, Class<?> relativeTo) throws IOException
Gets a file stored as a resource in a jar. Since not all users of the file can read from inside jars, we get the file as a stream and create a temp file with its contents.- Parameters:
name
-relativeTo
-- Returns:
- Throws:
IOException
- Since:
- 1.6
-
getFileURL
public static File getFileURL(String name, Class<?> relativeTo)
The getFile method of old, before we tried to fix it to work in JAR files.- Parameters:
name
-relativeTo
-- Returns:
- Since:
- 1.6
-
getDirectoryOf
public static File getDirectoryOf(Class<?> clazz) throws IOException
- Throws:
IOException
-
getPath
public static String getPath(String name, Class<?> relativeTo) throws IOException
- Throws:
IOException
-
checkFileExists
public static void checkFileExists(File file) throws FileNotFoundException
- Throws:
FileNotFoundException
-
copyToTemp
public static File copyToTemp(File srcFile) throws IOException
- Throws:
IOException
-
copy
public static void copy(File srcFile, File dstFile) throws IOException
- Throws:
IOException
-
sameContents
public static boolean sameContents(File fileExpected, File fileActual, Set<String> ignoreFilenames) throws IOException
We implement our own comparison algorithm here, so we don't need Eclipse Compare to compute differences, but rather only to show them in the UI.- Throws:
IOException
-
sameContents
public static boolean sameContents(InputStream isExpected, InputStream isActual) throws IOException
- Throws:
IOException
-
deleteDirectory
public static void deleteDirectory(String dir) throws IOException
- Parameters:
path
-- Throws:
IOException
- Since:
- 1.6
-
deleteDirectory
public static void deleteDirectory(Path path) throws IOException
WARNIING: Use with caution! Deletes all contents and sub-directories of the specified path.- Parameters:
dir
- The absolute path to the directory.- Throws:
IOException
- Since:
- 1.6
-
readDirectory
public static Map<Path,byte[]> readDirectory(String dir) throws IOException
- Parameters:
dir
-- Returns:
- Throws:
IOException
- Since:
- 1.6
-
readDirectory
public static Map<Path,byte[]> readDirectory(Path dir) throws IOException
Reads entire directory recursively, mapping the contents of each file as a string to its path.- Parameters:
dir
- The root directory.- Returns:
- The contents of each file in the directory and its subdirectories.
- Throws:
IOException
- Since:
- 1.6
-
createTempFile
public static File createTempFile(String name)
- Parameters:
name
-- Returns:
- Since:
- 1.6
-
createTempFile
public static File createTempFile(String name, String extension)
- Parameters:
name
-extension
-- Returns:
- Since:
- 1.6
-
-