java.lang.Object
imagingbook.core.FileUtils
This class defines various static methods for managing
file-based resources and JAR manifest files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkClass
(String classname) Checks 'by name' if a particular class exists.static void
copyToFile
(InputStream in, File file) Reads all data from the given input stream and copies them to to a file.static String
getClassPath
(Class<?> clazz) Find the path from which a given class was loaded.static String
Deprecated.static String
getCurrentDirectory
(Class<?> clazz) Returns the current directory associated with the specified class.static String
getFileExtension
(String name) Extracts the extension part of a pathname as a string.static Manifest
getJarManifest
(Class<?> clazz) Finds the manifest (from META-INF/MANIFEST.MF) of the JAR file from which clazz was loaded.static String
getModuleName
(Class<?> clazz) static String
getModuleName2
(Class<?> clazz) static String
static void
static void
Lists (to System.out) the paths where classes are loaded from.static File
selectFolder
(String startDirectory, String dialogTitle) Opens a dialog for the user to select a single folder (no files).static void
setCurrentDirectory
(Class<?> clazz, String pathname) Associates a current directory with the specified class by setting a system property to make this persistent through class reloads.static void
setCurrentDirectory
(Class<?> clazz, Path path) static void
setCurrentDirectory
(String pathname) Deprecated.static String
stripFileExtension
(String name) Removes the extension part of a pathname.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
stripFileExtension
Removes the extension part of a pathname. Examples:
"foo.txt" → "foo", "foo" → "foo", "foo." → "foo.", ".txt" → ".txt".- Parameters:
name
- the pathname- Returns:
- the pathname without the extension (if valid)
-
getFileExtension
Extracts the extension part of a pathname as a string. Examples:
"foo.txt" → "txt", "foo" → "", "foo." → "", ".txt" → "".- Parameters:
name
- the pathname- Returns:
- the extension or an empty string
-
getClassPath
Find the path from which a given class was loaded.- Parameters:
clazz
- a class.- Returns:
- the path of the .class file for the given class or null (e.g. if the class is anonymous).
-
printClassPath
Lists (to System.out) the paths where classes are loaded from. -
checkClass
Checks 'by name' if a particular class exists.- Parameters:
classname
- fully qualified name of the class, e.g. imagingbook.lib.util.FileUtils- Returns:
true
if the class was found,false
otherwise
-
copyToFile
Reads all data from the given input stream and copies them to to a file.- Parameters:
in
- the input streamfile
- the output file- Throws:
IOException
- if anything goes wrong
-
getJarManifest
Finds the manifest (from META-INF/MANIFEST.MF) of the JAR file from which clazz was loaded. See: http://stackoverflow.com/a/1273432- Parameters:
clazz
- A class in the JAR file of interest.- Returns:
- A
Manifest
object or null if clazz was not loaded from a JAR file.
-
selectFolder
Opens a dialog for the user to select a single folder (no files). Contained files and sub-folders are shown. Uses native (system) look-and-feel; original look-and-feel is restored.- Parameters:
startDirectory
- the directory to start from (pass""
or"."
for the current directory)dialogTitle
- the string shown in the title bar of the dialog window- Returns:
- a
File
object representing the selected directory ornull
if the dialog was canceled
-
setCurrentDirectory
Deprecated.Sets a system property to memorize the current directory. UsegetCurrentDirectory()
to retrieve this value. If the specified pathname is not a directory (i.e., a plain file), its parent directory is used.- Parameters:
pathname
- a directory of file path
-
setCurrentDirectory
Associates a current directory with the specified class by setting a system property to make this persistent through class reloads. UsegetCurrentDirectory(Class)
to retrieve this value. If the specified pathname is not a directory (i.e., a plain file), its parent directory is used.- Parameters:
clazz
- the class to associate the directory withpathname
- a directory of file path
-
setCurrentDirectory
-
getCurrentDirectory
Deprecated.Returns the current directory path. If the directory was set withsetCurrentDirectory(String)
before this path is returned, otherwise the value for the "user.dir" system property.- Returns:
- a string with the current directory path
-
getCurrentDirectory
Returns the current directory associated with the specified class. (usually of typePlugIn
orPlugInFilter
). If the directory was set withsetCurrentDirectory(Class,String)
before this path is returned,null
otherwise.- Parameters:
clazz
- the class to associate the directory with- Returns:
- a string with the current directory path or null if not registered
-
getTempDirectory
-
getModuleName
-
getModuleName2
- Throws:
URISyntaxException
-
main
- Throws:
URISyntaxException
-