|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.netarkivet.common.utils.FileUtils
public class FileUtils
Misc. handy file utilities.
Nested Class Summary | |
---|---|
static class |
FileUtils.FilenameParser
A class for parsing an ARC filename as generated by our runs of Heritrix and retrieving components like harvestID and jobID. |
Field Summary | |
---|---|
static java.lang.String |
ARC_EXTENSION
Extension used for ARC files, including separator . |
static java.lang.String |
ARC_GZIPPED_EXTENSION
Extension used for gzipped ARC files, including separator . |
static java.lang.String |
ARC_PATTERN
Pattern matching ARC files, including separator. |
static java.io.FilenameFilter |
ARCS_FILTER
A filter that matches arc files, that is any file that ends on .arc or .arc.gz in any case. |
static java.lang.String |
CDX_EXTENSION
Extension used for CDX files, including separator . |
static java.io.FilenameFilter |
CDX_FILE_FILTER
A FilenameFilter accepting a file if and only if its name (transformed to lower case) ends on ".cdx". |
static org.apache.commons.logging.Log |
log
The logger for this class. |
static int |
MAX_IDS_IN_FILENAME
Maximum number of IDs we will put in a filename. |
static java.lang.String |
OPEN_ARC_PATTERN
Pattern matching open ARC files, including separator . |
static java.io.FilenameFilter |
OPEN_ARCS_FILTER
A filter that matches files left open by a crashed Heritrix process. |
static java.lang.String |
WARC_GZIPPED_EXTENSION
Extension used for gzipped WARC files, including separator . |
static java.lang.String |
WARC_PATTERN
Pattern matching WARC files, including separator. |
static java.io.FilenameFilter |
WARCS_FILTER
A filter that matches warc files, that is any file that ends on .warc or .warc.gz in any case. |
Constructor Summary | |
---|---|
FileUtils()
|
Method Summary | ||
---|---|---|
static void |
appendToFile(java.io.File file,
java.lang.String... lines)
Append the given lines to a file. |
|
static void |
copyDirectory(java.io.File from,
java.io.File to)
Copy an entire directory from one location to another. |
|
static void |
copyFile(java.io.File from,
java.io.File to)
Copy file from one location to another. |
|
static long |
countLines(java.io.File file)
Count the number of lines in a file. |
|
static boolean |
createDir(java.io.File dir)
Check if the directory exists and is writable and create it if needed. |
|
static java.io.File |
createUniqueTempDir(java.io.File inDir,
java.lang.String prefix)
Creates a new temporary directory with a unique name. |
|
static java.lang.String |
formatFilename(java.lang.String filename)
Returns a valid filename for most filesystems. |
|
static
|
generateFileNameFromSet(java.util.Set<T> IDs,
java.lang.String suffix)
Given a set, generate a reasonable file name from the set. |
|
static long |
getBytesFree(java.io.File f)
Returns the number of bytes free on the file system calling the FreeSpaceProvider class defined by the setting CommonSettings.FREESPACE_PROVIDER_CLASS (a.k.a. |
|
static java.io.InputStream |
getEphemeralInputStream(java.io.File file)
Create an InputStream that reads from a file but removes the file when all data has been read. |
|
static java.util.List<java.io.File> |
getFilesRecursively(java.lang.String dir,
java.util.List<java.io.File> files,
java.lang.String type)
Retrieves all files whose names ends with 'type' from directory 'dir' and all its subdirectories. |
|
static java.io.File |
getResourceFileFromClassPath(java.lang.String filePath)
Loads an file from the class path (for retrieving a file from '.jar'). |
|
static java.io.File |
getTempDir()
Get the location of the standard temporary directory. |
|
static java.io.FilenameFilter |
getXmlFilesFilter()
Return a filter that only accepts XML files (ending with .xml), irrespective of their location. |
|
static void |
makeSortedFile(java.io.File unsortedFile,
java.io.File sortedOutput)
Sort a file into another. |
|
static java.io.File |
makeValidFileFromExisting(java.lang.String filename)
Makes a valid file from filename passed in String. |
|
static void |
moveFile(java.io.File fromFile,
java.io.File toFile)
Attempt to move a file using rename, and if that fails, move the file by copy-and-delete. |
|
static byte[] |
readBinaryFile(java.io.File file)
Read an entire file, byte by byte, into a byte array, ignoring any locale issues. |
|
static java.lang.String |
readFile(java.io.File file)
Load file content into text string. |
|
static java.lang.String |
readLastLine(java.io.File file)
Read the last line in a file. |
|
static java.util.List<java.lang.String> |
readListFromFile(java.io.File file)
Read a all lines from a file into a list of strings. |
|
static java.lang.String |
relativeTo(java.io.File theFile,
java.io.File theDir)
|
|
static boolean |
remove(java.io.File f)
Remove a file. |
|
static void |
removeLineFromFile(java.lang.String line,
java.io.File file)
Remove a line from a given file. |
|
static boolean |
removeRecursively(java.io.File f)
Remove a file and any subfiles in case of directories. |
|
static void |
sortCDX(java.io.File file,
java.io.File toFile)
Sort a CDX file according to our standard for CDX file sorting. |
|
static void |
sortCrawlLog(java.io.File file,
java.io.File toFile)
Sort a crawl.log file according to URL. |
|
static void |
writeBinaryFile(java.io.File file,
byte[] b)
Write an entire byte array to a file, ignoring any locale issues. |
|
static void |
writeCollectionToFile(java.io.File file,
java.util.Collection<java.lang.String> collection)
Writes a collection of strings to a file, each string on one line. |
|
static void |
writeFileToStream(java.io.File f,
java.io.OutputStream out)
Write the entire contents of a file to a stream. |
|
static void |
writeStreamToFile(java.io.InputStream in,
java.io.File f)
Write the contents of a stream into a file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String CDX_EXTENSION
public static final java.lang.String ARC_EXTENSION
public static final java.lang.String ARC_GZIPPED_EXTENSION
public static final java.lang.String WARC_GZIPPED_EXTENSION
public static final java.lang.String ARC_PATTERN
public static final java.lang.String OPEN_ARC_PATTERN
public static final java.lang.String WARC_PATTERN
public static final org.apache.commons.logging.Log log
public static final java.io.FilenameFilter CDX_FILE_FILTER
public static final java.io.FilenameFilter OPEN_ARCS_FILTER
public static final java.io.FilenameFilter ARCS_FILTER
public static final java.io.FilenameFilter WARCS_FILTER
public static final int MAX_IDS_IN_FILENAME
Constructor Detail |
---|
public FileUtils()
Method Detail |
---|
public static boolean removeRecursively(java.io.File f)
f
- A file to completely and utterly remove.
java.lang.SecurityException
- If a security manager exists and its SecurityManager.checkDelete(java.lang.String)
method denies delete access to the filepublic static boolean remove(java.io.File f)
f
- A file to completely and utterly remove.
ArgumentNotValid
- if f is null.
java.lang.SecurityException
- If a security manager exists and its SecurityManager.checkDelete(java.lang.String)
method denies delete access to the filepublic static java.lang.String formatFilename(java.lang.String filename)
filename
- the filename to format correctly
public static java.util.List<java.io.File> getFilesRecursively(java.lang.String dir, java.util.List<java.io.File> files, java.lang.String type)
dir
- Path of base directoryfiles
- Initially, an empty list (e.g. an ArrayList)type
- The extension/ending of the files to retrieve (e.g. ".xml",
".ARC")
public static java.lang.String readFile(java.io.File file) throws java.io.IOException
file
- The file to load
java.io.IOException
- If any IO trouble occurs while reading
the file, or the file cannot be found.public static void copyFile(java.io.File from, java.io.File to)
from
- original to copyto
- destination of copy
IOFailure
- if an io error occurs while copying file,
or the original file does not exist.public static void copyDirectory(java.io.File from, java.io.File to) throws IOFailure
from
- Original directory (or file, for that matter) to copy.to
- Destination directory, i.e. the 'new name' of the copy of the
from directory.
IOFailure
- On IO trouble copying files.public static byte[] readBinaryFile(java.io.File file) throws IOFailure, java.lang.IndexOutOfBoundsException
file
- A file to be read.
IOFailure
- on IO trouble reading the file,
or the file does not exist
java.lang.IndexOutOfBoundsException
- If the file is too large to be
in an array.public static void writeBinaryFile(java.io.File file, byte[] b)
file
- The file to write the data tob
- The byte array to write to the file
IOFailure
- If an exception occurs during the writing.public static java.io.FilenameFilter getXmlFilesFilter()
public static java.util.List<java.lang.String> readListFromFile(java.io.File file)
file
- The file to read from.
IOFailure
- on trouble reading the file,
or if the file does not existpublic static void writeCollectionToFile(java.io.File file, java.util.Collection<java.lang.String> collection)
file
- A file to write to. The contents of this file will be
overwritten.collection
- The collection to write. The order it will be
written in is unspecified.
IOFailure
- if any error occurs writing to the file.
ArgumentNotValid
- if file or collection is null.public static void makeSortedFile(java.io.File unsortedFile, java.io.File sortedOutput)
unsortedFile
- A file to sortsortedOutput
- The file to sort intopublic static void removeLineFromFile(java.lang.String line, java.io.File file)
line
- The full line to removefile
- The file to remove the line from. This file will be
rewritten in full, and the entire contents will be kept in memory
UnknownID
- If the file does not existpublic static boolean createDir(java.io.File dir) throws PermissionDenied
dir
- The directory to create
ArgumentNotValid
- If dir is null or its name is the empty string
PermissionDenied
- If directory cannot be created for any reason,
or is not writable.public static long getBytesFree(java.io.File f)
f
- a given file
public static java.lang.String relativeTo(java.io.File theFile, java.io.File theDir)
theFile
- A file to make relativetheDir
- A directory
public static long countLines(java.io.File file)
file
- the file to read
IOFailure
- If an error occurred while reading the filepublic static java.io.InputStream getEphemeralInputStream(java.io.File file)
file
- A file to read. This file will be deleted when the
inputstream is closed, finalized, reaches end-of-file, or when the
VM closes.
IOFailure
- If an error occurs in creating the ephemeral
input streampublic static java.io.File makeValidFileFromExisting(java.lang.String filename) throws IOFailure
filename
- The file to create the File object from
IOFailure
- if file cannot be created.public static void writeFileToStream(java.io.File f, java.io.OutputStream out)
f
- A file to write to the stream.out
- The stream to write to.
IOFailure
- If any error occurs while writing the file to a streampublic static void writeStreamToFile(java.io.InputStream in, java.io.File f)
in
- A stream to read from. This stream is not closed by this
method.f
- The file to write the stream contents into.
IOFailure
- If any error occurs while writing the stream to a filepublic static java.io.File getTempDir()
public static void moveFile(java.io.File fromFile, java.io.File toFile)
fromFile
- The sourcetoFile
- The targetpublic static <T extends java.lang.Comparable<T>> java.lang.String generateFileNameFromSet(java.util.Set<T> IDs, java.lang.String suffix)
T
- The type of objects, that the Set IDs argument contains.IDs
- A set of IDs.suffix
- A suffix. May be empty string.
public static void sortCrawlLog(java.io.File file, java.io.File toFile)
file
- The file containing the unsorted data.toFile
- The file that the sorted data can be put into.
IOFailure
- if there were errors running the sort process, or
if the file does not exist.public static void sortCDX(java.io.File file, java.io.File toFile)
file
- The raw unsorted CDX file.toFile
- The file that the result will be put into.
IOFailure
- If the file does not exist, or could not be sortedpublic static java.io.File createUniqueTempDir(java.io.File inDir, java.lang.String prefix)
inDir
- The directory where the temporary directory
should be created.prefix
- The prefix of the directory name, for identification
purposes.
ArgumentNotValid
- if inDir is not an existing
directory that can be written to.
IOFailure
- if a free name couldn't be found within a reasonable
number of tries.public static java.lang.String readLastLine(java.io.File file)
file
- input file to read last line from.
ArgumentNotValid
- on null argument, or file is not a readable
file.
IOFailure
- on IO trouble reading file.public static void appendToFile(java.io.File file, java.lang.String... lines)
file
- A file to append to.lines
- The lines to write.public static java.io.File getResourceFileFromClassPath(java.lang.String filePath) throws IOFailure
filePath
- The path of the file.
IOFailure
- If resource cannot be retrieved from the
class path.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |