Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    The Utility class to help file operating.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.w3c.dom.Element loadXMLFile​(java.lang.String filePath)
      Load the root element of an XML file.
      static org.w3c.dom.Element loadXMLFile​(java.lang.String filePath, Widget widget)
      Load the root element of an XML file.
      static void openFile​(java.lang.String filePath, Widget widget)
      Open a file in default editor.
      static java.lang.String openFileDialog​(boolean inWorkspace)
      Open a file select dialog.
      static java.util.List<java.lang.String> readTextFile​(java.lang.String filePath)
      Read a text file.
      static java.util.List<java.lang.String> readTextFile​(java.lang.String filePath, Widget widget)
      Read a text file.
      static java.lang.String saveFileDialog​(boolean inWorkspace)
      Open a file save dialog.
      static java.lang.String workspacePathToSysPath​(java.lang.String workspacePath)
      Convert a workspace path to system path.
      static void writeTextFile​(java.lang.String filePath, boolean inWorkspace, java.lang.String text, boolean append)
      Write a text file.
      static void writeTextFile​(java.lang.String filePath, boolean inWorkspace, Widget widget, java.lang.String text, boolean append)
      Write a text file.
      • Methods inherited from class java.lang.Object

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

      • FileUtil

        public FileUtil()
    • Method Detail

      • loadXMLFile

        public static org.w3c.dom.Element loadXMLFile​(java.lang.String filePath)
                                               throws java.lang.Exception
        Load the root element of an XML file. The element is a JDOM Element.
        Parameters:
        filePath - path of the file. It must be an absolute path which can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        a local file system path such as C:\myfile.xml
        or an URL path such as http://mysite.com/myfile.xml. *
        Returns:
        root element of the XML file.
        Throws:
        java.lang.Exception - if the file does not exist or is not a correct XML file.
      • loadXMLFile

        public static org.w3c.dom.Element loadXMLFile​(java.lang.String filePath,
                                                      Widget widget)
                                               throws java.lang.Exception
        Load the root element of an XML file. The element is a JDOM Element.
        Parameters:
        filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        a local file system path such as C:\myfile.xml
        or an URL path such as http://mysite.com/myfile.xml.
        widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
        Returns:
        root element of the XML file.
        Throws:
        java.lang.Exception - if the file does not exist or is not a correct XML file.
      • readTextFile

        public static java.util.List<java.lang.String> readTextFile​(java.lang.String filePath)
                                                             throws java.lang.Exception
        Read a text file.
        Parameters:
        filePath - path of the file. It must be an absolute path which can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        a local file system path such as C:\myfile.xml
        or an URL path such as http://mysite.com/myfile.xml.
        Returns:
        a string of the text.
        Throws:
        java.lang.Exception - if the file does not exist or is not a correct text file.
      • readTextFile

        public static java.util.List<java.lang.String> readTextFile​(java.lang.String filePath,
                                                                    Widget widget)
                                                             throws java.lang.Exception
        Read a text file.
        Parameters:
        filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        a local file system path such as C:\myfile.xml
        or an URL path such as http://mysite.com/myfile.xml.
        widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
        Returns:
        a string of the text.
        Throws:
        java.lang.Exception - if the file does not exist or is not a correct text file.
      • writeTextFile

        public static void writeTextFile​(java.lang.String filePath,
                                         boolean inWorkspace,
                                         java.lang.String text,
                                         boolean append)
                                  throws java.lang.Exception
        Write a text file.
        Parameters:
        filePath - path of the file. It must be an absolute path which can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        or a local file system path such as C:\myfile.xml.
        inWorkspace - true if the file path is a workspace file path. Otherwise, it will be recognized as a local file system file.
        text - the text to be written to the file.
        append - true if the text should be appended to the end of the file.
        Throws:
        java.lang.Exception - if error happens.
      • writeTextFile

        public static void writeTextFile​(java.lang.String filePath,
                                         boolean inWorkspace,
                                         Widget widget,
                                         java.lang.String text,
                                         boolean append)
                                  throws java.lang.Exception
        Write a text file.
        Parameters:
        filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
        a workspace path such as /BOY Examples/Scripts/myfile.xml
        a local file system path such as C:\myfile.xml
        or an URL path such as http://mysite.com/myfile.xml.
        inWorkspace - true if the file path is a workspace file path. Otherwise, it will be recognized as a local file system file.
        widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
        text - the text to be written to the file.
        append - true if the text should be appended to the end of the file.
        Throws:
        java.lang.Exception - if error happens.
      • openFile

        public static void openFile​(java.lang.String filePath,
                                    Widget widget)
        Open a file in default editor. If no such an editor for the type of file, OS default program will be called to open this file.
        Parameters:
        filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either a workspace path such as
        /BOY Examples/Scripts/myfile.txt
        or a local file system path such as C:\myfile.txt.
        widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
      • openFileDialog

        public static java.lang.String openFileDialog​(boolean inWorkspace)
        Open a file select dialog.
        Parameters:
        inWorkspace - true if it is a workspace file dialog; Otherwise, it is a local file system file dialog.
        Returns:
        the full file path. Or null if it is cancelled.
      • saveFileDialog

        public static java.lang.String saveFileDialog​(boolean inWorkspace)
        Open a file save dialog.
        Parameters:
        inWorkspace - true if it is a workspace file dialog; Otherwise, it is a local file system file dialog.
        Returns:
        the full file path. Or null if it is cancelled.
      • workspacePathToSysPath

        public static java.lang.String workspacePathToSysPath​(java.lang.String workspacePath)
                                                       throws java.lang.RuntimeException
        Convert a workspace path to system path. If this resource is a project that does not exist in the workspace, or a file or folder below such a project, this method returns null.
        Parameters:
        workspacePath - path in workspace.
        Returns:
        the system path on OS. Return an empty string if the path doesn't exist.
        Throws:
        java.lang.RuntimeException