Reputation: 787
I have a configuration file from which I have to read some properties. This configuration file exists in diferent locations but has the same name. I am able to read a configuration file, but only one from my project. I used the following code:
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
try {
prop.load(inputStream);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (inputStream == null) {
try {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have to open the config file using the full path so instead of propFileName="config.properties" to insert the absolute path to the config file. The config file can't be opened if I use the absolute path. How can this be done ?
Upvotes: 1
Views: 21714
Reputation: 11786
You can use FileInputStream for locating config file outside of your project path.
Properties prop = new Properties();
InputStream input = new FileInputStream("/home/ubuntu/Desktop/sample.properties");
prop.load(input);
System.out.println(prop.get("test"));
When specifying path we need give file name with extension.
Upvotes: 3
Reputation: 111142
InputStream inputStream = new FileInputStream("path");
will open a file with an absolute path.
Note: This will only work for a file, it will not work for anything included in a plugin jar.
Upvotes: 4