Package elki.utilities.io
Class FileUtil
- java.lang.Object
-
- elki.utilities.io.FileUtil
-
public final class FileUtil extends java.lang.ObjectVarious static helper methods to deal with files and file names.- Since:
- 0.2
- Author:
- Erich Schubert
-
-
Constructor Summary
Constructors Modifier Constructor Description privateFileUtil()Fake Constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanexists(java.net.URI file)Check if the file/resource identified by an URI exists.static java.lang.StringgetFilenameExtension(java.lang.String name)Returns the lower case extension of the selected file.static java.lang.StringgetFilenameExtension(java.nio.file.Path file)Returns the lower case extension of the selected file.static java.io.FilelocateFile(java.lang.String name, java.lang.String basedir)Try to locate an file in the filesystem, given a partial name and a prefix.static java.io.InputStreamopen(java.net.URI file, java.nio.file.OpenOption... opts)Open a file identified by an URI for reading.static java.io.InputStreamopenSystemFile(java.lang.String filename)Try to open a file, first trying the file system, then falling back to the classpath.static java.lang.Stringslurp(java.io.InputStream is)Load an input stream (e.g., a Java resource) into a String buffer.static java.io.InputStreamtryGzipInput(java.io.InputStream in)Try to open a stream as gzip, if it starts with the gzip magic.
-
-
-
Method Detail
-
getFilenameExtension
public static java.lang.String getFilenameExtension(java.nio.file.Path file)
Returns the lower case extension of the selected file.If no file is selected,
nullis returned.- Parameters:
file- File object- Returns:
- Returns the extension of the selected file in lower case or
null
-
getFilenameExtension
public static java.lang.String getFilenameExtension(java.lang.String name)
Returns the lower case extension of the selected file.If no file is selected,
nullis returned.- Parameters:
name- File name- Returns:
- Returns the extension of the selected file in lower case or
null
-
open
public static java.io.InputStream open(java.net.URI file, java.nio.file.OpenOption... opts) throws java.io.IOExceptionOpen a file identified by an URI for reading.- Parameters:
file- Fileopts- Open options- Returns:
- File input stream
- Throws:
java.io.IOException- on error
-
exists
public static boolean exists(java.net.URI file)
Check if the file/resource identified by an URI exists.- Parameters:
file- File URI- Returns:
trueif the file exists.
-
openSystemFile
public static java.io.InputStream openSystemFile(java.lang.String filename) throws java.io.IOExceptionTry to open a file, first trying the file system, then falling back to the classpath.- Parameters:
filename- File name in system notation- Returns:
- Input stream
- Throws:
java.io.IOException
-
tryGzipInput
public static java.io.InputStream tryGzipInput(java.io.InputStream in) throws java.io.IOExceptionTry to open a stream as gzip, if it starts with the gzip magic.- Parameters:
in- original input stream- Returns:
- old input stream or a
GZIPInputStreamif appropriate. - Throws:
java.io.IOException- on IO error
-
locateFile
public static java.io.File locateFile(java.lang.String name, java.lang.String basedir)Try to locate an file in the filesystem, given a partial name and a prefix.- Parameters:
name- file namebasedir- extra base directory to try- Returns:
- file, if the file could be found.
nullotherwise
-
slurp
public static java.lang.String slurp(java.io.InputStream is) throws java.io.IOExceptionLoad an input stream (e.g., a Java resource) into a String buffer. The stream is closed afterwards.- Parameters:
is- Input stream- Returns:
- String with file/resource contents.
- Throws:
java.io.IOException- on IO errors
-
-