Package com.peterphi.std.io
Class FileHelper
java.lang.Object
com.peterphi.std.io.FileHelper
Title: File Helper
Description: File-related helper functions
Copyright: Copyright (c) 2006-2009
- Version:
- $Revision$
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic Stringcat(InputStream is) static Stringstatic Stringstatic Stringstatic Stringstatic voidstatic booleancouldWrite(File f) static FilecreateTempFile(String prefix, String suffix) Creates a temporary file namestatic booleanDeletes a local file or directory from the filesystemstatic StringenforceSafeFilename(String filename) Ensures that a file is "safe" (this is done by whitelisting a small number of characters)
This method is mainly designed for producing a filename which bears some resemblance to a user-supplied String while removing the risk of being trickedstatic booleanstatic booleanisAbsoluteFile(String filename) static booleanisAncestor(File root, File f) static voidCreate a directory and any necessary parent directories, throwing aRuntimeExceptionon failurestatic voidstatic longReads a PID (Process Id) filestatic longReads a PID (Process Id) filestatic booleanDeprecated.use commons file utils FileUtils.moveDirectoryToDirectory insteadstatic booleansmartEquals(File one, File two, boolean checkName) Determines if 2 files or directories are equivalent by looking inside themstatic voidstatic voidCreates/updates a file with the modify date set to nowstatic booleantrySymlink(File from, File to) static voidstatic voidstatic voidwrite(File f, StringBuilder sb) static voidstatic void
-
Method Details
-
createTempFile
Creates a temporary file name- Returns:
- File
-
enforceSafeFilename
Ensures that a file is "safe" (this is done by whitelisting a small number of characters)
This method is mainly designed for producing a filename which bears some resemblance to a user-supplied String while removing the risk of being tricked- Parameters:
filename-- Returns:
-
isAbsoluteFile
-
isAbsoluteFile
-
isAncestor
-
couldWrite
-
copy
- Throws:
IOException
-
move
- Throws:
IOException
-
trySymlink
-
symlink
- Throws:
IOException
-
safeMove
Deprecated.use commons file utils FileUtils.moveDirectoryToDirectory insteadSafely moves a file from one place to another, ensuring the filesystem is left in a consistent state- Parameters:
src- File The source filedest- File The destination file- Returns:
- boolean True if the file has been completely moved to the new location, false if it is still in the original location
- Throws:
SecurityException- MAY BE THROWN if permission is denied to src or dest
-
delete
Deletes a local file or directory from the filesystem- Parameters:
f- File The file/directory to delete- Returns:
- boolean True if the deletion was a success, otherwise false
- Throws:
IOException
-
smartEquals
Determines if 2 files or directories are equivalent by looking inside them- Parameters:
one- File The first file/directorytwo- File The second file/directorycheckName- boolean Whether names should be identical also- Returns:
- boolean True if the files/directories are equivalent, otherwise false
- Throws:
IOException- On an unhandleable error or a non-file, non-directory input
-
readPID
Reads a PID (Process Id) file- Parameters:
f- File The process Id file (must exist!)- Returns:
- String The process Id represented by the file (or -1 if the file doesn't exist)
- Throws:
IOException- On filesystem-level errors
-
readPID
Reads a PID (Process Id) file- Parameters:
f- File The process Id file (must exist!)carefulProcessing- boolean If true, non-numeric chars are stripped from the PID before it is parsed- Returns:
- String The process Id represented by the file (or -1 if the file doesn't exist)
- Throws:
IOException- On filesystem-level errors
-
writePID
- Throws:
IOException
-
writePID
- Throws:
IOException
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
cat
- Throws:
IOException
-
cat
- Throws:
IOException
-
cat
- Throws:
IOException
-
cat
- Throws:
IOException
-
cat
- Throws:
IOException
-
cat
- Throws:
IOException
-
touch
Creates/updates a file with the modify date set to now- Parameters:
f-- Throws:
IOException
-
mkdirs
Create a directory and any necessary parent directories, throwing aRuntimeExceptionon failure- Parameters:
f- the directory to create- Throws:
RuntimeException- wrapping the inner IOException if the filesystem cannot create a directory
-