54 lines
1.8 KiB
Java
54 lines
1.8 KiB
Java
|
|
package com.utopiadeals.utils;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import java.io.*;
|
|
import java.util.Properties;
|
|
|
|
import static java.lang.ClassLoader.getSystemResourceAsStream;
|
|
|
|
public class FileIoHelper {
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
/**
|
|
* Loads properties from a file in the classpath
|
|
* @param filePath Path to the properties file in the classpath
|
|
* @return Properties object containing the loaded properties
|
|
*/
|
|
public static Properties loadPropertiesFromClasspath(String filePath) {
|
|
Properties properties = new Properties();
|
|
try (InputStream in = FileIoHelper.class.getClassLoader().getResourceAsStream(filePath)) {
|
|
if (in != null) {
|
|
properties.load(in);
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Failed to load properties from: " + filePath, e);
|
|
}
|
|
return properties;
|
|
}
|
|
|
|
/**
|
|
* Loads a JSON file from the classpath or file system
|
|
* @param filePath Path to the JSON file (can be in classpath or filesystem)
|
|
* @return JsonNode containing the parsed JSON
|
|
*/
|
|
public static JsonNode loadJsonFile(String filePath) {
|
|
try {
|
|
// Try to load from classpath first
|
|
try (InputStream inputStream = FileIoHelper.class.getClassLoader().getResourceAsStream(filePath)) {
|
|
if (inputStream != null) {
|
|
return mapper.readTree(inputStream);
|
|
}
|
|
}
|
|
|
|
// Fall back to file system
|
|
return mapper.readTree(new File(filePath));
|
|
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Failed to load JSON file: " + filePath, e);
|
|
}
|
|
}
|
|
}
|