java.lang.Object
org.openpatch.scratch.extensions.fs.File

public class File extends Object
The File class provides methods to save and load objects to and from files in JSON or XML format. It supports saving and loading objects with automatic detection of file type based on the method used. The class uses Jackson ObjectMapper for JSON and XmlMapper for XML serialization and deserialization.

Usage examples:


 // Save an object as JSON
 File.save("/path/to/file.json", myObject);

 // Save an object as XML
 File.saveXML("/path/to/file.xml", myObject);

 // Load an object from JSON
 MyClass obj = File.load("/path/to/file.json", MyClass.class);

 // Load an object from XML
 MyClass obj = File.loadXML("/path/to/file.xml", MyClass.class);
 

Note: The file paths can be absolute or relative. If a relative path is provided, the class will attempt to load the file from the classpath.

Supported file types:

  • JSON
  • XML
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    load(String path, Class<T> cls)
    Loads an object of the specified class from a file at the given path.
    static <T> T
    loadXML(String path, Class<T> cls)
    Loads an object of the specified class from an XML file at the given path.
    static <T> void
    save(String path, T obj)
    Saves the given object to the specified path in the default file type (JSON).
    static <T> void
    saveXML(String path, T obj)
    Saves the given object as an XML file at the specified path.

    Methods inherited from class java.lang.Object

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

    • File

      public File()
  • Method Details

    • save

      public static <T> void save(String path, T obj)
      Saves the given object to the specified path in the default file type (JSON).
      Type Parameters:
      T - the type of the object to be saved
      Parameters:
      path - the path where the object should be saved
      obj - the object to be saved
    • saveXML

      public static <T> void saveXML(String path, T obj)
      Saves the given object as an XML file at the specified path.
      Type Parameters:
      T - the type of the object to be saved
      Parameters:
      path - the path where the XML file will be saved
      obj - the object to be saved as XML
    • load

      public static <T> T load(String path, Class<T> cls)
      Loads an object of the specified class from a file at the given path.
      Type Parameters:
      T - the type of the object to be loaded
      Parameters:
      path - the path to the file from which the object is to be loaded
      cls - the class of the object to be loaded
      Returns:
      the loaded object of the specified class
    • loadXML

      public static <T> T loadXML(String path, Class<T> cls)
      Loads an object of the specified class from an XML file at the given path.
      Type Parameters:
      T - the type of the object to be loaded
      Parameters:
      path - the path to the XML file
      cls - the class of the object to be loaded
      Returns:
      the object loaded from the XML file