Reputation: 2001
Am trying to ready properties file which is presented in my project directory src/test/resources/properties/api/
. But this way is not working and its give me file not found exception.
Please find my code below :
public Properties extractProperties() throws IOException {
InputStream configReader= null;
String env = getProperty("tuf.environment");
try {
configReader = new FileInputStream(new File("src/test/resources/properties/api/"+env+".properties")); // throwing exception
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prop.load(configReader);
return prop;
}
Upvotes: 0
Views: 517
Reputation: 186
Try something like below
public Properties extractProperties() throws IOException {
Properties prop=new Properties();
String env = getProperty("tuf.environment");
String mappingFileName = "/properties/api/" + env+ ".properties";
Resource resource = resourceLoader.getResource("classpath:" + mappingFileName);
try (InputStream inputStream = resource.getInputStream();
BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(inputStream))) {
prop.load(bufferedInputStream);
} catch IOException ie) {
//handle exception
}
return prop;
}
Upvotes: 0
Reputation: 124556
Judging from your path you are using either Maven or Gradle as it looks like the default structure used by them. Which means src/test/resources
points to the root of the classpath, so there is no src/test/resources
. (The same applies to src/main/resources
as well!).
So if you want to load it yuo would need to remove the src/test/resources
part of the loading.
Next if this is run from a packaged application loading a File
won't work as it isn't a File
. The File
needs to be a physical file on the filesystem and not inside an archive.
Taking all that into account you should be able to load the properties using the following
public Properties extractProperties() throws IOException {
String env = getProperty("tuf.environment");
String resource = "/properties/api/"+env+".properties";
try (InputStream in = getClass().getResourceAsStream(resource)) {
prop.load(in);
return prop;
}
}
Upvotes: 1
Reputation: 1393
I would do it the following way. Please note that the extractProperties()
method will return an empty Properties
object if the file was not found. Please also note the try-with-resources
statement which will auto-close the InputStream
.
public Properties extractProperties() throws IOException {
String env = getProperty("tuf.environment");
Properties prop = new Properties();
try (InputStream in = this.getClass().getResourceAsStream("/properties/api/" + env + ".properties")) {
prop.load(in);
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
Upvotes: 2
Reputation: 327
Probably env
is not what you think it is. Why not list all files in that directory?
You can print with https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/nio/file/Files.html#list(java.nio.file.Path)
With the relevant directory:
Path apiDir = Paths.get("src/test/resources/properties/api/");
Upvotes: -1