Package ui.main

Class FileManager


  • final class FileManager
    extends java.lang.Object
    The FileManager class is used for managing files.
    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      FileManager()
      Constructs a FileManager object initialised with a default directory and file extensions.
      FileManager​(java.io.File initialDirectory, java.lang.String... validFileExtensions)
      Constructs a FileManager object initialised with an initial directory and defined valid file extensions.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean checkIfSaved​(java.lang.String text)
      Returns a boolean indicating if a file has been saved.
      java.lang.String openFile()
      Opens a FileChooser window, allowing the user to open a file.
      java.lang.String openNewFile()
      Creates a new file by returning an empty string.
      void saveFile​(java.lang.String text)
      Saves a file without opening a FileChooser window.
      void saveFileAs​(java.lang.String text)
      Opens a FileChooser window, allowing the user to name and save a file.
      void saveFileAs​(java.lang.String text, java.lang.String initialFileName)
      Opens a FileChooser window with a specified initial file name, allowing the user to name and save a file.
      • Methods inherited from class java.lang.Object

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

      • FileManager

        public FileManager​(java.io.File initialDirectory,
                           java.lang.String... validFileExtensions)
        Constructs a FileManager object initialised with an initial directory and defined valid file extensions.

        The initial directory is the location the user is first taken to when opening a FileChooser window.

        The valid file extensions provide a list of file types which the user is allowed to open. Note that provided Strings must not include the initial period, as this is applied already.

        Parameters:
        initialDirectory - The initial directory presented to the user when opening a FileChooser window
        validFileExtensions - A list of valid file types the user can open or save as.
      • FileManager

        public FileManager()
        Constructs a FileManager object initialised with a default directory and file extensions.

        This method sets the initialDirectory to the users 'documents' folder, and allows txt file extensions.

    • Method Detail

      • openNewFile

        public java.lang.String openNewFile()
        Creates a new file by returning an empty string.
        Returns:
        An empty String
      • openFile

        public java.lang.String openFile()
                                  throws java.io.IOException,
                                         java.lang.NullPointerException,
                                         java.lang.IllegalArgumentException
        Opens a FileChooser window, allowing the user to open a file.

        When the user has chosen a file, its contents will be returned by this method.

        The FileChooser will initially open the folder defined by the initialDirectory field. Furthermore, the user will only be able to selected files defined in the validFileExtensions list.

        Returns:
        A String containing the contents of the file
        Throws:
        java.io.IOException - If the selected file doesn't exist, or the user does not have permission to read the file
        java.lang.NullPointerException - If no file was selected
        java.lang.IllegalArgumentException - If the file extension was invalid
      • saveFileAs

        public void saveFileAs​(java.lang.String text)
                        throws java.io.IOException,
                               java.lang.NullPointerException,
                               java.lang.IllegalArgumentException
        Opens a FileChooser window, allowing the user to name and save a file.

        The user will only be able to save a file with an extension defined in the validFileExtensions list.

        This method sets the initial file name to "new-script.txt" by default.

        Parameters:
        text - The text to be written to the file
        Throws:
        java.io.IOException - If the file could not be written to
        java.lang.NullPointerException - If no file was selected
        java.lang.IllegalArgumentException - If the file extension was invalid
      • saveFileAs

        public void saveFileAs​(java.lang.String text,
                               java.lang.String initialFileName)
                        throws java.io.IOException,
                               java.lang.NullPointerException,
                               java.lang.IllegalArgumentException
        Opens a FileChooser window with a specified initial file name, allowing the user to name and save a file.

        The user will only be able to save a file with an extension defined in the validFileExtensions list.

        Parameters:
        text - The text to be written to the file
        initialFileName - The initial file name of the file being saved
        Throws:
        java.io.IOException - If the file could not be written to
        java.lang.NullPointerException - If no file was selected
        java.lang.IllegalArgumentException - If the file extension was invalid
      • saveFile

        public void saveFile​(java.lang.String text)
                      throws java.io.IOException,
                             java.lang.NullPointerException,
                             java.lang.IllegalArgumentException
        Saves a file without opening a FileChooser window.

        This method will only run if the current file has not been modified in any way, such as the file being moved, renamed, deleted, or updated. If this is the case, the saveFileAs method will run instead.

        Parameters:
        text - The text to be written to the file
        Throws:
        java.io.IOException - If the file could not be written to
        java.lang.NullPointerException - If no file was selected
        java.lang.IllegalArgumentException - If the file extension was invalid
      • checkIfSaved

        public boolean checkIfSaved​(java.lang.String text)
        Returns a boolean indicating if a file has been saved.

        The input text is compared against the last saved or opened file to see if their contents match. If they do, the file must have been saved.

        Parameters:
        text - The text being checked to see if its been saved
        Returns:
        A boolean indicating whether the text has been saved or not