Class FileHelper

java.lang.Object
com.peterphi.std.io.FileHelper

public class FileHelper extends Object

Title: File Helper

Description: File-related helper functions

Copyright: Copyright (c) 2006-2009

Version:
$Revision$
  • Method Details

    • createTempFile

      public static File createTempFile(String prefix, String suffix)
      Creates a temporary file name
      Returns:
      File
    • enforceSafeFilename

      public static 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 tricked
      Parameters:
      filename -
      Returns:
    • isAbsoluteFile

      public static boolean isAbsoluteFile(File f)
    • isAbsoluteFile

      public static boolean isAbsoluteFile(String filename)
    • isAncestor

      public static boolean isAncestor(File root, File f)
    • couldWrite

      public static boolean couldWrite(File f)
    • copy

      public static void copy(File src, File dest) throws IOException
      Throws:
      IOException
    • move

      public static void move(File src, File dest) throws IOException
      Throws:
      IOException
    • trySymlink

      public static boolean trySymlink(File from, File to)
    • symlink

      public static void symlink(File from, File to) throws IOException
      Throws:
      IOException
    • safeMove

      @Deprecated public static boolean safeMove(File src, File dest) throws SecurityException
      Deprecated.
      use commons file utils FileUtils.moveDirectoryToDirectory instead
      Safely moves a file from one place to another, ensuring the filesystem is left in a consistent state
      Parameters:
      src - File The source file
      dest - 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

      public static boolean delete(File f) throws IOException
      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

      public static boolean smartEquals(File one, File two, boolean checkName) throws IOException
      Determines if 2 files or directories are equivalent by looking inside them
      Parameters:
      one - File The first file/directory
      two - File The second file/directory
      checkName - 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

      public static long readPID(File f) throws IOException
      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

      public static long readPID(File f, boolean carefulProcessing) throws IOException
      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

      public static void writePID(File f, long pid) throws IOException
      Throws:
      IOException
    • writePID

      public static void writePID(File f, String pid) throws IOException
      Throws:
      IOException
    • write

      public static void write(File f, String contents) throws IOException
      Throws:
      IOException
    • write

      public static void write(File f, StringBuilder sb) throws IOException
      Throws:
      IOException
    • write

      public static void write(File f, Reader fr) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(String filename) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(InputStream is) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(URL u) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(Reader reader) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(File f) throws IOException
      Throws:
      IOException
    • cat

      public static String cat(Reader reader, int sizeEstimate) throws IOException
      Throws:
      IOException
    • touch

      public static void touch(File f) throws IOException
      Creates/updates a file with the modify date set to now
      Parameters:
      f -
      Throws:
      IOException
    • mkdirs

      public static void mkdirs(File f)
      Create a directory and any necessary parent directories, throwing a RuntimeException on failure
      Parameters:
      f - the directory to create
      Throws:
      RuntimeException - wrapping the inner IOException if the filesystem cannot create a directory