Class FileUtils
java.lang.Object
org.archive.util.FileUtils
Utility methods for manipulating files and directories.
- Author:
- John Erik Halse, gojomo
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic FileTest file exists and is readable.static booleanCopy the src file to the destination.static booleanCopy up to extent bytes of the source file to the destination.static booleanCopy up to extent bytes of the source file to the destinationstatic voiddeleteSoonerOrLater(File fileToDelete) Delete the file now -- but in the event of failure, keep trying in the future.static FileEnsure writeable directory.static FileEnsure writeable directory.ensureWriteableDirectory(List<File> dirs) Ensure writeable directories.static org.apache.commons.lang3.LongRangeexpandRange(org.apache.commons.lang3.LongRange range1, org.apache.commons.lang3.LongRange range2) static File[]getFilesWithPrefix(File dir, String prefix) Get a list of all files in directory that have passed prefix.static org.apache.commons.io.filefilter.IOFileFiltergetRegexFileFilter(String regex) Get a @link java.io.FileFilter that filters files based on a regular expression.static booleanisReadableWithExtensionAndMagic(File f, String uncompressedExtension, String magic) static PropertiesloadProperties(File file) Load Properties instance from a Filestatic FilemaybeRelative(File context, String path) Turn path into a File, relative to context (which may be ignored if path is absolute).static booleanmoveAsideIfExists(File file) static org.apache.commons.lang3.LongRangepagedLines(File file, long position, int signedDesiredLongCount, List<String> lines) static org.apache.commons.lang3.LongRangepagedLines(File file, long position, int signedDesiredLineCount, List<String> lines, int lineEstimate) Retrieve a number of lines from the file around the given position, as when paging forward or backward through a file.static longreadFullyToFile(InputStream is, File toFile) Read the entire stream to EOF into the passed file.static voidstoreProperties(Properties p, File file) Store Properties instance to a Filestatic FiletryToCanonicalize(File file) protected static voidworkaroundCopyFile(File src, File dest)
-
Field Details
-
pendingDeletes
-
-
Method Details
-
copyFile
Copy the src file to the destination. Deletes any preexisting file at destination.- Parameters:
src-dest-- Returns:
- True if the extent was greater than actual bytes copied.
- Throws:
FileNotFoundExceptionIOException
-
copyFile
public static boolean copyFile(File src, File dest, long extent) throws FileNotFoundException, IOException Copy up to extent bytes of the source file to the destination. Deletes any preexisting file at destination.- Parameters:
src-dest-extent- Maximum number of bytes to copy- Returns:
- True if the extent was greater than actual bytes copied.
- Throws:
FileNotFoundExceptionIOException
-
copyFile
public static boolean copyFile(File src, File dest, long extent, boolean overwrite) throws FileNotFoundException, IOException Copy up to extent bytes of the source file to the destination- Parameters:
src-dest-extent- Maximum number of bytes to copyoverwrite- If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).- Returns:
- True if the extent was greater than actual bytes copied.
- Throws:
FileNotFoundExceptionIOException
-
workaroundCopyFile
- Throws:
IOException
-
getFilesWithPrefix
-
getRegexFileFilter
Get a @link java.io.FileFilter that filters files based on a regular expression.- Parameters:
regex- the regular expression the files must match.- Returns:
- the newly created filter.
-
assertReadable
Test file exists and is readable.- Parameters:
f- File to test.- Throws:
FileNotFoundException- If file does not exist or is not unreadable.
-
isReadableWithExtensionAndMagic
public static boolean isReadableWithExtensionAndMagic(File f, String uncompressedExtension, String magic) throws IOException - Parameters:
f- File to test.- Returns:
- True if file is readable, has uncompressed extension, and magic string at file start.
- Throws:
IOException- If file not readable or other problem.
-
maybeRelative
-
loadProperties
Load Properties instance from a File- Parameters:
file-- Returns:
- Properties
- Throws:
IOException
-
storeProperties
Store Properties instance to a File- Parameters:
p-file- destination File- Throws:
IOException
-
moveAsideIfExists
- Throws:
IOException
-
pagedLines
public static org.apache.commons.lang3.LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines, int lineEstimate) throws IOException Retrieve a number of lines from the file around the given position, as when paging forward or backward through a file.- Parameters:
file- File to retrieve linesposition- offset to anchor linessignedDesiredLineCount- lines requested; if negative, want this number of lines ending with a line containing the position; if positive, want this number of lines, all starting at or after position.lines- list to insert found lineslineEstimate- int estimate of line size, 0 means use default of 128- Returns:
- LongRange indicating the file offsets corresponding to the beginning of the first line returned, and the point after the end of the last line returned
- Throws:
IOException
-
expandRange
public static org.apache.commons.lang3.LongRange expandRange(org.apache.commons.lang3.LongRange range1, org.apache.commons.lang3.LongRange range2) -
pagedLines
public static org.apache.commons.lang3.LongRange pagedLines(File file, long position, int signedDesiredLongCount, List<String> lines) throws IOException - Throws:
IOException
-
deleteSoonerOrLater
Delete the file now -- but in the event of failure, keep trying in the future. VERY IMPORTANT: Do not use with any file whose name/path may be reused, because the lagged delete could then wind up deleting the newer file. Essentially, only to be used with uniquely-named temp files. Necessary because some platforms (looking at you, JVM-on-Windows) will have deletes fail because of things like file-mapped buffers remaining, and there's no explicit way to unmap a buffer. (See 6-year-old Sun-stumping Java bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 ) We just have to wait and retry. (Why not just File.deleteOnExit? There could be an arbitrary, unbounded number of files in such a situation, that are only deletable a few seconds or minutes after our first attempt. Waiting for JVM exist could mean disk exhaustion. It's also unclear if the native FS class implementations of deleteOnExit use RAM per pending file.)- Parameters:
fileToDelete-
-
readFullyToFile
Read the entire stream to EOF into the passed file. Closesiswhen done or if an exception.- Parameters:
is- Stream to read.toFile- File to write to.- Throws:
IOException
-
ensureWriteableDirectory
Ensure writeable directory. If doesn't exist, we attempt creation.- Parameters:
dir- Directory to test for exitence and is writeable.- Returns:
- The passed
dir. - Throws:
IOException- If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
-
ensureWriteableDirectory
Ensure writeable directories. If doesn't exist, we attempt creation.- Parameters:
dirs- List of Files to test.- Returns:
- The passed
dirs. - Throws:
IOException- If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
-
ensureWriteableDirectory
Ensure writeable directory. If doesn't exist, we attempt creation.- Parameters:
dir- Directory to test for exitence and is writeable.- Returns:
- The passed
dir. - Throws:
IOException- If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
-
tryToCanonicalize
-
appendTo
- Throws:
IOException
-