Package com.mooltiverse.oss.nyx.io
Class FileMapper
- java.lang.Object
-
- com.mooltiverse.oss.nyx.io.FileMapper
-
public class FileMapper extends Object
This class is used to load and save data files like configuration or state files.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tload(File file, Class<T> type)Unmarshals the content of the given file to an object of the given type.static <T> Tload(URL url, Class<T> type)Unmarshals the content of the given URL to an object of the given type.static voidsave(String filePath, Object content)Marshals the content of the given object to a file represented by the given path.
-
-
-
Method Detail
-
load
public static <T> T load(File file, Class<T> type) throws DataAccessException
Unmarshals the content of the given file to an object of the given type.- Type Parameters:
T- the type of object to unmarshal.- Parameters:
file- the file to load from. The file path must end with one of the supported extensions:json,yaml,yml(or JSON is used by default).type- the class representing the type of object to unmarshal.- Returns:
- the object instance unmarshalled from the given file.
- Throws:
DataAccessException- in case of any exception due to data accessIllegalArgumentException- if the given file path does not contain a supported extension
-
load
public static <T> T load(URL url, Class<T> type) throws DataAccessException
Unmarshals the content of the given URL to an object of the given type.- Type Parameters:
T- the type of object to unmarshal.- Parameters:
url- the URL to load from. The file path must end with one of the supported extensions:json,yaml,yml(or JSON is used by default).type- the class representing the type of object to unmarshal.- Returns:
- the object instance unmarshalled from the given URL.
- Throws:
DataAccessException- in case of any exception due to data accessIllegalArgumentException- if the given file path does not contain a supported extension
-
save
public static void save(String filePath, Object content) throws DataAccessException
Marshals the content of the given object to a file represented by the given path.- Parameters:
filePath- the path of the file to save to. If it's a relative path it will be considered relative to the current working directory. The file path must end with one of the supported extensions:json,yaml,yml(or JSON is used by default).content- the object to marshal.- Throws:
DataAccessException- in case of any exception due to data accessIllegalArgumentException- if the given file path does not contain a supported extension
-
-