public final class LKFileUtils extends Object
File utilities for LensKit. Called LKFileUtils to avoid conflict with FileUtils classes that may be imported from other packages such as Guava, Plexus, or Commons.
Modifier and Type | Method and Description |
---|---|
static String |
basename(String path,
boolean keepExt)
Get the basename of a file path, possibly without extension.
|
static ByteSource |
byteSource(File file)
Create a file byte source, automatically decompressing based on file name.
|
static ByteSource |
byteSource(File file,
CompressionMode compression)
Create a file byte source.
|
static ByteSource |
byteSource(URL url,
CompressionMode compression)
Create a URL-backed byte source.
|
static boolean |
isCompressed(File file)
Deprecated.
Use
CompressionMode or commons-compress facilities instead. |
static Reader |
openInput(File file)
Open a reader with automatic compression and the default character set.
|
static Reader |
openInput(File file,
Charset charset,
CompressionMode compression)
Open a file for input, optionally compressed.
|
static Reader |
openInput(File file,
CompressionMode compression)
Open a file for input with the default charset.
|
static Writer |
openOutput(File file)
Open a reader with automatic compression inference.
|
static Writer |
openOutput(File file,
Charset charset,
CompressionMode compression)
Open a file for input, optionally compressed.
|
static Writer |
openOutput(File file,
CompressionMode compression)
Open a file for output with the default charset.
|
static LongList |
readIdList(File file)
Read a list of long IDs from a file, one per line.
|
static InputStream |
transparentlyDecompress(InputStream stream)
Auto-detect whether a stream needs decompression.
|
@Deprecated public static boolean isCompressed(File file)
CompressionMode
or commons-compress facilities instead.Query whether this filename represents a compressed file. It just looks at the name to see if it ends in “.gz”.
file
- The file to query.true
if the file name ends in “.gz”.public static Reader openInput(File file, Charset charset, CompressionMode compression) throws IOException
Open a file for input, optionally compressed.
file
- The file to open.charset
- The character set to use.compression
- Whether to compress the file.IOException
- if there is an error opening the file.public static Reader openInput(File file, CompressionMode compression) throws IOException
Open a file for input with the default charset.
file
- The file to open.compression
- The compression mode.IOException
- if there was an error opening the file.openInput(java.io.File, Charset, CompressionMode)
public static Reader openInput(File file) throws IOException
Open a reader with automatic compression and the default character set.
file
- The file to open.IOException
- if there is an error opening the file.openInput(File, Charset, CompressionMode)
,
CompressionMode.AUTO
,
Charset.defaultCharset()
public static Writer openOutput(File file, Charset charset, CompressionMode compression) throws IOException
Open a file for input, optionally compressed.
file
- The file to open.charset
- The character set to use.compression
- Whether to compress the file.IOException
- if there is an error opening the file.public static Writer openOutput(File file, CompressionMode compression) throws IOException
Open a file for output with the default charset.
file
- The file to open.compression
- The compression mode.IOException
- if there was an error opening the file.openInput(java.io.File, Charset, CompressionMode)
public static Writer openOutput(File file) throws IOException
Open a reader with automatic compression inference.
file
- The file to open.IOException
- if there is an error opening the file.public static ByteSource byteSource(File file)
Create a file byte source, automatically decompressing based on file name.
file
- The file byte source.public static ByteSource byteSource(File file, CompressionMode compression)
Create a file byte source.
file
- The file containing data.compression
- The compression mode.public static ByteSource byteSource(URL url, CompressionMode compression)
Create a URL-backed byte source.
url
- The URL of the byte source.compression
- The compression mode.public static InputStream transparentlyDecompress(@WillCloseWhenClosed InputStream stream) throws IOException
Auto-detect whether a stream needs decompression. Currently detects GZIP compression (using the GZIP magic in the header).
stream
- The stream to read.stream
, decompressing if needed. It may not be the same object as stream
, even if no decompression is needed, as the input stream may be wrapped in a buffered stream for lookahead.IOException
public static LongList readIdList(File file) throws IOException
Read a list of long IDs from a file, one per line.
file
- The file to read.IOException