public final class LKFileUtils
extends java.lang.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 java.lang.String |
basename(java.lang.String path,
boolean keepExt)
Get the basename of a file path, possibly without extension.
|
static ByteSource |
byteSource(java.io.File file)
Create a file byte source, automatically decompressing based on file name.
|
static ByteSource |
byteSource(java.io.File file,
CompressionMode compression)
Create a file byte source.
|
static ByteSource |
byteSource(java.net.URL url,
CompressionMode compression)
Create a URL-backed byte source.
|
static boolean |
isCompressed(java.io.File file)
Deprecated.
Use
CompressionMode or commons-compress facilities instead. |
static java.io.Reader |
openInput(java.io.File file)
Open a reader with automatic compression and the default character set.
|
static java.io.Reader |
openInput(java.io.File file,
java.nio.charset.Charset charset,
CompressionMode compression)
Open a file for input, optionally compressed.
|
static java.io.Reader |
openInput(java.io.File file,
CompressionMode compression)
Open a file for input with the default charset.
|
static java.io.Writer |
openOutput(java.io.File file)
Open a reader with automatic compression inference.
|
static java.io.Writer |
openOutput(java.io.File file,
java.nio.charset.Charset charset,
CompressionMode compression)
Open a file for input, optionally compressed.
|
static java.io.Writer |
openOutput(java.io.File file,
CompressionMode compression)
Open a file for output with the default charset.
|
static LongList |
readIdList(java.io.File file)
Read a list of long IDs from a file, one per line.
|
static java.io.InputStream |
transparentlyDecompress(java.io.InputStream stream)
Auto-detect whether a stream needs decompression.
|
@Deprecated public static boolean isCompressed(java.io.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 java.io.Reader openInput(java.io.File file, java.nio.charset.Charset charset, CompressionMode compression) throws java.io.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.java.io.IOException
- if there is an error opening the file.public static java.io.Reader openInput(java.io.File file, CompressionMode compression) throws java.io.IOException
Open a file for input with the default charset.
file
- The file to open.compression
- The compression mode.java.io.IOException
- if there was an error opening the file.openInput(java.io.File, Charset, CompressionMode)
public static java.io.Reader openInput(java.io.File file) throws java.io.IOException
Open a reader with automatic compression and the default character set.
file
- The file to open.java.io.IOException
- if there is an error opening the file.openInput(File, Charset, CompressionMode)
,
CompressionMode.AUTO
,
Charset.defaultCharset()
public static java.io.Writer openOutput(java.io.File file, java.nio.charset.Charset charset, CompressionMode compression) throws java.io.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.java.io.IOException
- if there is an error opening the file.public static java.io.Writer openOutput(java.io.File file, CompressionMode compression) throws java.io.IOException
Open a file for output with the default charset.
file
- The file to open.compression
- The compression mode.java.io.IOException
- if there was an error opening the file.openInput(java.io.File, Charset, CompressionMode)
public static java.io.Writer openOutput(java.io.File file) throws java.io.IOException
Open a reader with automatic compression inference.
file
- The file to open.java.io.IOException
- if there is an error opening the file.public static ByteSource byteSource(java.io.File file)
Create a file byte source, automatically decompressing based on file name.
file
- The file byte source.public static ByteSource byteSource(java.io.File file, CompressionMode compression)
Create a file byte source.
file
- The file containing data.compression
- The compression mode.public static ByteSource byteSource(java.net.URL url, CompressionMode compression)
Create a URL-backed byte source.
url
- The URL of the byte source.compression
- The compression mode.public static java.io.InputStream transparentlyDecompress(@WillCloseWhenClosed java.io.InputStream stream) throws java.io.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.java.io.IOException
public static LongList readIdList(java.io.File file) throws java.io.IOException
Read a list of long IDs from a file, one per line.
file
- The file to read.java.io.IOException
public static java.lang.String basename(java.lang.String path, boolean keepExt)
Get the basename of a file path, possibly without extension.
path
- The file path.keepExt
- Whether to keep the extension.