Class File
java.lang.Object
org.openpatch.scratch.extensions.fs.File
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Loads an object of the specified class from a file at the given path.static <T> T
Loads an object of the specified class from an XML file at the given path.static <T> void
Saves the given object to the specified path in the default file type (JSON).static <T> void
Saves the given object as an XML file at the specified path.
-
Constructor Details
-
File
public File()
-
-
Method Details
-
save
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 savedobj
- the object to be saved
-
saveXML
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 savedobj
- the object to be saved as XML
-
load
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 loadedcls
- the class of the object to be loaded- Returns:
- the loaded object of the specified class
-
loadXML
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 filecls
- the class of the object to be loaded- Returns:
- the object loaded from the XML file
-