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 String
static String
cat
(InputStream is) static String
static String
static String
static String
static void
static boolean
couldWrite
(File f) static File
createTempFile
(String prefix, String suffix) Creates a temporary file namestatic boolean
Deletes a local file or directory from the filesystemstatic String
enforceSafeFilename
(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 boolean
static boolean
isAbsoluteFile
(String filename) static boolean
isAncestor
(File root, File f) static void
Create a directory and any necessary parent directories, throwing aRuntimeException
on failurestatic void
static long
Reads a PID (Process Id) filestatic long
Reads a PID (Process Id) filestatic boolean
Deprecated.use commons file utils FileUtils.moveDirectoryToDirectory insteadstatic boolean
smartEquals
(File one, File two, boolean checkName) Determines if 2 files or directories are equivalent by looking inside themstatic void
static void
Creates/updates a file with the modify date set to nowstatic boolean
trySymlink
(File from, File to) static void
static void
static void
write
(File f, StringBuilder sb) static void
static 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 aRuntimeException
on failure- Parameters:
f
- the directory to create- Throws:
RuntimeException
- wrapping the inner IOException if the filesystem cannot create a directory
-