Class FileUtils

  • All Implemented Interfaces:
    java.io.Serializable

    public class FileUtils
    extends java.lang.Object
    implements java.io.Serializable
    Title: FileUtils class Description: Includes generic functions to use when accessing files and directories. All methods are Static so no class instance is required to access them. Copyright: Copyright (c) 2001 Company: MicroStrategy, Inc.
    Since:
    MicroStrategy Web 7.3.1 or earlier
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String cleanFileName​(java.lang.String s)
      Produces a clean form of a file name (typically from an object name, such as the name of a report).
      static java.lang.String cleanPathName​(java.lang.String s)
      Produces a clean form of a path name.
      static boolean fileExists​(java.lang.String fileName)
      Verifies if the file can be accessed.
      static long fileSize​(java.lang.String fileName)
      Given the file path and name, searches for the file and returns its size
      static java.lang.String getTextFileContents​(java.lang.String fileName)
      Opens the indicated file.
      static java.lang.String getTextFileContents​(java.lang.String fileName, boolean newLine)
      Obtains the contents of the requested file
      static java.lang.String getTextFileContents​(java.lang.String fileName, boolean newLine, java.lang.String encoding)
      Obtains the contents of the requested file
      static java.lang.String getTextFileContents​(java.lang.String fileName, boolean newLine, java.lang.String encoding, java.lang.String newLineChar)
      Obtains the contents of the requested file
      static java.lang.String getTextFileContentsWithoutBom​(java.lang.String fileName, boolean newLine)
      Obtains the contents of the requested file, if encoded in UTF-8, delete the BOM
      static void saveObjectFileContents​(java.lang.String fileName, FileContentsObjectList oInfo, boolean append)
      Writes to the indicated file.
      static void setObjectFileContents​(java.lang.String fileName, FileContentsObjectList oInfo)
      Saves the collection sent as parameter into the indicated file.
      static void setObjectFileContents​(java.lang.String fileName, FileContentsObjectList oInfo, boolean append)
      Saves the oInfo collection into the indicated file.
      static void setTextFileContents​(java.lang.String fileName, java.lang.String fileContents)
      Saves the string sent as parameter into the indicated file.
      static void setTextFileContents​(java.lang.String fileName, java.lang.String fileContents, boolean append)
      Opens the requested file as a new one (overwrite) or appending the contents if requested.
      static void setTextFileContents​(java.lang.String fileName, java.lang.String fileContents, boolean changeLine, boolean append)
      Opens the requested file as a new one (overwrite) or appending the contents if requested.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • getTextFileContents

        public static java.lang.String getTextFileContents​(java.lang.String fileName,
                                                           boolean newLine)
                                                    throws WebAppException
        Obtains the contents of the requested file
        Parameters:
        fileName - name of the file to search for
        newLine - if it should insert a change of line each time it finds one on the file
        Returns:
        a String with the file contents
        Throws:
        WebAppException - if an error happened while reading the file.
      • getTextFileContents

        public static java.lang.String getTextFileContents​(java.lang.String fileName,
                                                           boolean newLine,
                                                           java.lang.String encoding)
                                                    throws WebAppException
        Obtains the contents of the requested file
        Parameters:
        fileName - name of the file to search for
        newLine - if it should insert a change of line each time it finds one on the file
        encoding - encoding type
        Returns:
        a String with the file contents
        Throws:
        WebAppException - if an error happened while reading the file.
      • getTextFileContents

        public static java.lang.String getTextFileContents​(java.lang.String fileName,
                                                           boolean newLine,
                                                           java.lang.String encoding,
                                                           java.lang.String newLineChar)
                                                    throws WebAppException
        Obtains the contents of the requested file
        Parameters:
        fileName - name of the file to search for
        newLine - if it should insert a change of line each time it finds one on the file
        encoding - encoding type
        newLineChar - the new line separator
        Returns:
        a String with the file contents
        Throws:
        WebAppException - if an error happened while reading the file.
      • getTextFileContentsWithoutBom

        public static java.lang.String getTextFileContentsWithoutBom​(java.lang.String fileName,
                                                                     boolean newLine)
                                                              throws WebAppException
        Obtains the contents of the requested file, if encoded in UTF-8, delete the BOM
        Parameters:
        fileName - name of the file to search for
        newLine - if it should insert a change of line each time it finds one on the file
        Returns:
        a String with the file contents
        Throws:
        WebAppException - if an error happened while reading the file.
      • getTextFileContents

        public static java.lang.String getTextFileContents​(java.lang.String fileName)
                                                    throws WebAppException
        Opens the indicated file. Starts reading it line by line and storing its content on a buffer. When the end of the file is reached, the buffer is copied into a String which is the output of this method. Handles java.io.FileNotFoundException if the file requested is not found. Catches Exception if any other error happens while reading the file contents.
        Parameters:
        fileName - Name of the file from where the contents are to be extracted.
        Returns:
        String with the file contents on it (including line breaks)
        Throws:
        WebAppException - if any error happens while accessing the file.
      • fileExists

        public static boolean fileExists​(java.lang.String fileName)
        Verifies if the file can be accessed.
        Parameters:
        fileName - Name of the file to search for.
        Returns:
        true if the file is found, otherwise returns false
      • fileSize

        public static long fileSize​(java.lang.String fileName)
        Given the file path and name, searches for the file and returns its size
        Parameters:
        fileName - path and file name to search for
        Returns:
        the size of the file. If the file was not found or there was an error while loading it, returns 0.
      • setTextFileContents

        public static void setTextFileContents​(java.lang.String fileName,
                                               java.lang.String fileContents,
                                               boolean append)
                                        throws WebAppException
        Opens the requested file as a new one (overwrite) or appending the contents if requested. Writes the string and closes the file. By default changes line after inserting contents.
        Parameters:
        fileName - Name of the file where to write.
        fileContents - String to write to the file.
        append - Boolean indicating if the file is to be appended or overwritten
        Throws:
        WebAppException - if any error happens while accessing the file.
      • setTextFileContents

        public static void setTextFileContents​(java.lang.String fileName,
                                               java.lang.String fileContents,
                                               boolean changeLine,
                                               boolean append)
                                        throws WebAppException
        Opens the requested file as a new one (overwrite) or appending the contents if requested. Writes the string and closes the file.
        Parameters:
        fileName - Name of the file where to write.
        fileContents - String to write to the file.
        changeLine - Boolean indicating if a change of line should be inserted after the contents or not
        append - Boolean indicating if the file is to be appended or overwritten
        Throws:
        WebAppException - if any error happens while accessing the file.
      • setTextFileContents

        public static void setTextFileContents​(java.lang.String fileName,
                                               java.lang.String fileContents)
                                        throws WebAppException
        Saves the string sent as parameter into the indicated file. By default it overwrites the file if already existing. Used as interface for saveTextFileContents. By default appends an end of line after inserting the contents.
        Parameters:
        fileName - Name of the file where to write.
        fileContents - String to write to the file
        Throws:
        WebAppException - if any error happens while accessing the file.
      • saveObjectFileContents

        public static void saveObjectFileContents​(java.lang.String fileName,
                                                  FileContentsObjectList oInfo,
                                                  boolean append)
                                           throws WebAppException
        Writes to the indicated file. Receives a FileContentsObjectList collection where all the information to be saved is found. It parses each one of the elements of the list and writes it to the file.
        Parameters:
        fileName - Name of the file from where the contents are to be extracted.
        oInfo - FileContentsObjectList collection with the contents to be written to the file
        append - Boolean indicating if the file should be appended or overwritten *
        Throws:
        WebAppException - if any error happens while accessing the file.
      • setObjectFileContents

        public static void setObjectFileContents​(java.lang.String fileName,
                                                 FileContentsObjectList oInfo)
                                          throws WebAppException
        Saves the collection sent as parameter into the indicated file. By default it overwrites the file if already existing. Used as interface for saveObjectFileContents.
        Parameters:
        fileName - Name of the file where to write.
        oInfo - FileContentsObjectList instance to write back to the file
        Throws:
        WebAppException - if any error happens while accessing the file.
      • setObjectFileContents

        public static void setObjectFileContents​(java.lang.String fileName,
                                                 FileContentsObjectList oInfo,
                                                 boolean append)
                                          throws WebAppException
        Saves the oInfo collection into the indicated file. By default it overwrites the file if already existing. Used as interface for saveObjectFileContents.
        Parameters:
        fileName - Name of the file where to write.
        oInfo - FileContentsObjectList instance to write back to the file
        append - Boolean indicating if the file should be appended or overwritten
        Throws:
        WebAppException - if any error happens while accessing the file.
      • cleanPathName

        public static java.lang.String cleanPathName​(java.lang.String s)
        Produces a clean form of a path name. This is really just a method to replace any non-printing control chars in a string with another character ('_') and then replaces all occurences of "\.." and "/.." with just "\" or "/". This is to prevent unexpected transvesal of file system via path name. This function does not exclude other chars to be inclusive for multiple OS
        Parameters:
        s - This is the incoming string that may have control characters in it.
        Returns:
        the String with all control characters replaced.
      • cleanFileName

        public static java.lang.String cleanFileName​(java.lang.String s)
        Produces a clean form of a file name (typically from an object name, such as the name of a report). This is really just a method to replace a predefined set of characters ('%', '.', '/', '\\', ':', '*', '?', '"', '<', '>', '|', ',') in a string with another character ('_').
        Parameters:
        s - This is the incoming string that may have special characters in it.
        Returns:
        the String with all special characters replaced.