Modifier and Type | Field and Description |
---|---|
static Pattern |
TAB_DELIMITED |
static String |
UTF8 |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
classpath2Filepath(String path) |
static InputStream |
classpathStream(String path) |
static Set<String> |
columnsToSet(InputStream source,
int... column) |
static Set<String> |
columnsToSet(InputStream source,
Set<String> resultSet,
int... column)
Reads a file and returns a unique set of multiple columns from lines which are no comments (starting with #) and
trims whitespace.
|
static void |
copyStreams(InputStream in,
OutputStream out) |
static void |
copyStreamToFile(InputStream in,
File out) |
static void |
createParentDirs(File file)
Creates any necessary but nonexistent parent directories of the specified file.
|
static File |
createTempDir() |
static File |
createTempDir(String prefix,
String suffix) |
static void |
deleteDirectoryRecursively(File directory)
Delete directory recursively, including all its files, sub-folders, and sub-folder's files.
|
static String |
escapeFilename(String filename)
Escapes a filename so it is a valid filename on all systems, replacing /.
|
static File |
getClasspathFile(String path) |
static String |
getFileExtension(String fullName)
Returns the file extension for
the given file name, or the empty string if the file has no extension.
|
static InputStream |
getInputStream(File source) |
static BufferedReader |
getInputStreamReader(InputStream input) |
static BufferedReader |
getInputStreamReader(InputStream input,
String encoding) |
static org.apache.commons.io.LineIterator |
getLineIterator(InputStream source) |
static org.apache.commons.io.LineIterator |
getLineIterator(InputStream source,
String encoding) |
int |
getLinesPerMemorySort() |
static BufferedReader |
getUtf8Reader(File file) |
static String |
humanReadableByteCount(long bytes,
boolean si)
Converts the byte size into human-readable format.
|
static boolean |
isCompressedFile(File source) |
void |
mergeSortedFiles(List<File> sortFiles,
Writer sortedFileWriter,
Comparator<String> lineComparator)
Merges a list of intermediary sort chunk files into a single sorted file.
|
static ByteBuffer |
readByteBuffer(File file)
Reads a complete file into a byte buffer.
|
static ByteBuffer |
readByteBuffer(File file,
int bufferSize)
Reads the first bytes of a file into a byte buffer.
|
static void |
setLinesPerMemorySort(int linesPerMemorySort) |
void |
sort(File input,
File sorted,
String encoding,
int column,
String columnDelimiter,
Character enclosedBy,
String newlineDelimiter,
int ignoreHeaderLines)
Sorts the input file into the output file using the supplied delimited line parameters.
|
void |
sort(File input,
File sorted,
String encoding,
int column,
String columnDelimiter,
Character enclosedBy,
String newlineDelimiter,
int ignoreHeaderLines,
Comparator<String> lineComparator,
boolean ignoreCase)
Sorts the input file into the output file using the supplied delimited line parameters.
|
void |
sort(List<File> inputs,
File sorted,
String encoding,
int column,
String columnDelimiter,
Character enclosedBy,
String newlineDelimiter,
int ignoreHeaderLines)
Sorts the input file into the output file using the supplied delimited line parameters.
|
void |
sort(List<File> inputs,
File sorted,
String encoding,
int column,
String columnDelimiter,
Character enclosedBy,
String newlineDelimiter,
int ignoreHeaderLines,
Comparator<String> lineComparator,
boolean ignoreCase)
Sorts the input file into the output file using the supplied delimited line parameters.
|
void |
sortInJava(File input,
File sorted,
String encoding,
Comparator<String> lineComparator,
int ignoreHeaderLines)
Sorts the input file into the output file using the supplied lineComparator.
|
void |
sortInJava(List<File> inputs,
File sorted,
String encoding,
Comparator<String> lineComparator,
int ignoreHeaderLines)
Sorts the input file into the output file using the supplied lineComparator.
|
List<File> |
split(File input,
int linesPerOutput,
String extension)
Splits the supplied file into files of set line size and with a suffix.
|
static Writer |
startNewUtf8File(File file) |
static Writer |
startNewUtf8XmlFile(File file) |
static LinkedList<String> |
streamToList(InputStream source)
Takes a utf8 encoded input stream and reads in every line/row into a list.
|
static List<String> |
streamToList(InputStream source,
List<String> resultList)
Reads a file and returns a list of all lines which are no comments (starting with #) and trims whitespace.
|
static LinkedList<String> |
streamToList(InputStream source,
String encoding) |
static Map<String,String> |
streamToMap(InputStream source)
Reads a utf8 encoded inut stream, splits
|
static Map<String,String> |
streamToMap(InputStream source,
int key,
int value,
boolean trimToNull) |
static Map<String,String> |
streamToMap(InputStream source,
Map<String,String> result)
Read a hashmap from a tab delimited utf8 input stream using the row number as an integer value and the entire row
as the value.
|
static Map<String,String> |
streamToMap(InputStream source,
Map<String,String> result,
int key,
int value,
boolean trimToNull)
Read a hashmap from a tab delimited utf8 file, ignoring commented rows starting with #.
|
static Set<String> |
streamToSet(InputStream source) |
static Set<String> |
streamToSet(InputStream source,
Set<String> resultSet)
Reads a file and returns a unique set of all lines which are no comments (starting with #) and trims whitespace.
|
static String |
toFilePath(URL url) |
static void |
touch(File file)
Creates an empty file or updates the last updated timestamp on the same as the unix command of
the same name.
|
static File |
url2file(URL url) |
public static final Pattern TAB_DELIMITED
public FileUtils()
public static String classpath2Filepath(String path)
public static InputStream classpathStream(String path) throws IOException
IOException
public static Set<String> columnsToSet(InputStream source, int... column) throws IOException
IOException
public static Set<String> columnsToSet(InputStream source, Set<String> resultSet, int... column) throws IOException
source
- the UTF-8 encoded text file with tab delimited columnsresultSet
- the set implementation to be used. Will not be cleared before reading!column
- variable length argument of column indices to processIOException
public static void copyStreams(InputStream in, OutputStream out) throws IOException
IOException
public static void copyStreamToFile(InputStream in, File out) throws IOException
IOException
public static File createTempDir() throws IOException
IOException
public static File createTempDir(String prefix, String suffix) throws IOException
prefix
- The prefix string to be used in generating the file's name; must be at least three characters longsuffix
- The suffix string to be used in generating the file's name; may be null, in which case the suffix
".tmp" will be usedIOException
public static void deleteDirectoryRecursively(File directory)
directory
- directory to delete recursivelypublic static String escapeFilename(String filename)
filename
- to be escapedpublic static File getClasspathFile(String path)
public static InputStream getInputStream(File source) throws FileNotFoundException
FileNotFoundException
public static BufferedReader getInputStreamReader(InputStream input) throws FileNotFoundException
FileNotFoundException
public static BufferedReader getInputStreamReader(InputStream input, String encoding) throws FileNotFoundException
FileNotFoundException
public static org.apache.commons.io.LineIterator getLineIterator(InputStream source)
source
- the source input stream encoded in UTF-8public static org.apache.commons.io.LineIterator getLineIterator(InputStream source, String encoding)
source
- the source input streamencoding
- the encoding used by the input streampublic static BufferedReader getUtf8Reader(File file) throws FileNotFoundException
FileNotFoundException
public static String humanReadableByteCount(long bytes, boolean si)
public static boolean isCompressedFile(File source)
public static ByteBuffer readByteBuffer(File file) throws IOException
IOException
public static ByteBuffer readByteBuffer(File file, int bufferSize) throws IOException
bufferSize
- the number of bytes to read from the fileIOException
public static void setLinesPerMemorySort(int linesPerMemorySort)
linesPerMemorySort
- are the number of lines that should be sorted in memory, determining the number of file
segments to be sorted when doing a Java file sort. Defaults to 100000, if you have
memory available a higher value increases performance.public static Writer startNewUtf8File(File file) throws IOException
IOException
public static Writer startNewUtf8XmlFile(File file) throws IOException
IOException
public static LinkedList<String> streamToList(InputStream source) throws IOException
IOException
public static List<String> streamToList(InputStream source, List<String> resultList) throws IOException
source
- the UTF-8 encoded text file to readresultList
- the list implementation to be used. Will not be cleared before reading!IOException
public static LinkedList<String> streamToList(InputStream source, String encoding) throws IOException
IOException
public static Map<String,String> streamToMap(InputStream source) throws IOException
IOException
public static Map<String,String> streamToMap(InputStream source, int key, int value, boolean trimToNull) throws IOException
IOException
public static Map<String,String> streamToMap(InputStream source, Map<String,String> result) throws IOException
source
- tab delimited text file to readIOException
public static Map<String,String> streamToMap(InputStream source, Map<String,String> result, int key, int value, boolean trimToNull) throws IOException
source
- tab delimited input stream to readkey
- column number to use as keyvalue
- column number to use as valuetrimToNull
- if true trims map entries to nullIOException
public static Set<String> streamToSet(InputStream source) throws IOException
IOException
public static Set<String> streamToSet(InputStream source, Set<String> resultSet) throws IOException
source
- the UTF-8 encoded text file to readresultSet
- the set implementation to be used. Will not be cleared before reading!IOException
public static String toFilePath(URL url)
public int getLinesPerMemorySort()
public void mergeSortedFiles(List<File> sortFiles, Writer sortedFileWriter, Comparator<String> lineComparator) throws IOException
sortFiles
- sort chunk files to mergesortedFileWriter
- writer to merge to. Can already be open and contain datalineComparator
- To use when determining the order (reuse the one that was used to sort the individual
files)IOException
public void sort(File input, File sorted, String encoding, int column, String columnDelimiter, Character enclosedBy, String newlineDelimiter, int ignoreHeaderLines) throws IOException
input
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)column
- the column that keeps the values to sort oncolumnDelimiter
- the delimiter that separates columns in a rowenclosedBy
- optional column enclosing character, e.g. a double quote for CSVsnewlineDelimiter
- the chars used for new lines, usually \n, \n\r or \rignoreHeaderLines
- number of beginning lines to ignore, e.g. headersIOException
public void sort(List<File> inputs, File sorted, String encoding, int column, String columnDelimiter, Character enclosedBy, String newlineDelimiter, int ignoreHeaderLines) throws IOException
inputs
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)column
- the column that keeps the values to sort oncolumnDelimiter
- the delimiter that separates columns in a rowenclosedBy
- optional column enclosing character, e.g. a double quote for CSVsnewlineDelimiter
- the chars used for new lines, usually \n, \n\r or \rignoreHeaderLines
- number of beginning lines to ignore, e.g. headersIOException
public void sort(File input, File sorted, String encoding, int column, String columnDelimiter, Character enclosedBy, String newlineDelimiter, int ignoreHeaderLines, Comparator<String> lineComparator, boolean ignoreCase) throws IOException
input
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)column
- the column that keeps the values to sort oncolumnDelimiter
- the delimiter that separates columns in a rowenclosedBy
- optional column enclosing character, e.g. a double quote for CSVsnewlineDelimiter
- the chars used for new lines, usually \n, \r\n or \rignoreHeaderLines
- number of beginning lines to ignore, e.g. headerslineComparator
- used to sort the outputignoreCase
- ignore case order, this parameter couldn't have any effect if the LineComparator is usedIOException
public void sort(List<File> inputs, File sorted, String encoding, int column, String columnDelimiter, Character enclosedBy, String newlineDelimiter, int ignoreHeaderLines, Comparator<String> lineComparator, boolean ignoreCase) throws IOException
inputs
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)column
- the column that keeps the values to sort oncolumnDelimiter
- the delimiter that separates columns in a rowenclosedBy
- optional column enclosing character, e.g. a double quote for CSVsnewlineDelimiter
- the chars used for new lines, usually \n, \r\n or \rignoreHeaderLines
- number of beginning lines to ignore, e.g. headerslineComparator
- used to sort the outputignoreCase
- ignore case order, this parameter couldn't have any effect if the LineComparator is usedIOException
public void sortInJava(File input, File sorted, String encoding, Comparator<String> lineComparator, int ignoreHeaderLines) throws IOException
input
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)lineComparator
- To use during comparisonignoreHeaderLines
- number of beginning lines to ignore, e.g. headersIOException
public void sortInJava(List<File> inputs, File sorted, String encoding, Comparator<String> lineComparator, int ignoreHeaderLines) throws IOException
inputs
- To sortsorted
- The sorted version of the input excluding ignored header lines (see ignoreHeaderLines)lineComparator
- To use during comparisonignoreHeaderLines
- number of beginning lines to ignore, e.g. headersIOException
public List<File> split(File input, int linesPerOutput, String extension) throws IOException
input
- To split uplinesPerOutput
- Lines per split fileextension
- The file extension to use - e.g. ".txt"IOException
public static void touch(File file) throws IOException
From Guava.
file
- the file to create or updateIOException
- if an I/O error occurspublic static String getFileExtension(String fullName)
.
'.
Note: This method simply returns everything after the last '.
' in the file's
name as determined by File.getName()
. It does not account for any filesystem-specific
behavior that the File
API does not already account for. For example, on NTFS it will
report "txt"
as the extension for the filename "foo.exe:.txt"
even though NTFS
will drop the ":.txt"
part of the name when the file is actually created on the
filesystem due to NTFS's Alternate Data Streams.
From Guava.
public static void createParentDirs(File file) throws IOException
From Guava.
IOException
- if an I/O error occurs, or if any necessary but nonexistent parent
directories of the specified file could not be created.Copyright © 2024 Global Biodiversity Information Facility (GBIF). All rights reserved.