Where can I place configuration file in Android project? Currently I am made a directory in res directory as :
res/config/configuration.properties
and want to access it as :
properties = new Properties();
properties.load(getClass().getResourceAsStream("config/configuration.properties"));
It is not working : input stream is null
You can place it in assets directory as assets/configuration.properties
Example code
try {
InputStream is = context.getAssets().open("configuration.properties");
Properties props = new Properties();
props.load(is);
String value = props.getProperty("key", "");
is.close();
} catch (Exception e) {
}
Properties properties = new Properties();
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("config.properties");
properties.load(inputStream);
properties.getProperty(key);
Related
I have a global.properties file and have to define the file path inside this properties file.
SheetPath=C:\\Users\\test\\Automation-Scripts\\DataTable.xlsx
This is an absolute path but require a way to define a relative path that can be consumed while calling.
Properties file:
testPath=API_Files/duplicateToken.json
Load the properties file:
public static Properties readProperties = new Properties();
public static void loadPropertiesFile() {
File propertiesFile = new File(location of properties file);
try {
FileInputStream fileInput = new FileInputStream(propertiesFile);
readProperties.load(fileInput);
} catch (Exception e) {
Logger.LogError("Error in loading the Properties file" + e.getMessage());
}
}
Read the properties file and get absolute path:
String testPath = readProperties.getProperty("testPath").trim();
File absolutePath = new File(System.getProperty("user.dir") + testPath);
System.out.println(absolutePath);
Sample output:
C:\Users\test\Automation-Scripts\duplicateToken.json
The properties file should be relative to classpath. You can create a "configs" folder at the level of src and use the below code to read the file. Refer this for more explanation and techniques.
private Properties properties;
private final String propertyFilePath= "configs//Configuration.properties";
BufferedReader reader = new BufferedReader(new FileReader(propertyFilePath));
Properties properties = new Properties();
properties.load(reader);
I'm executing a jar file which reads configs from a config file outside of /home/user/xxx/testFolder/jarfile, the path of config file is /opt/xxx/conf/global_config.cfg.
However, I'm able to access files inside the jar, so I assume the error is due to the file not being found.
Below is my code:
public Properties createProperties(){
Properties p = null;
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream = cl.getResourceAsStream("/opt/xxx/conf/global_config.cfg")) {
p = new Properties();
BufferedInputStream bis = new BufferedInputStream(stream);
p.load(bis); // this is throwing the error
System.out.println(p.toString());
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
What is the correct way of getting a file regardless of its path in a Linux system?
cl.getResourceAsStream("/opt/xxx/conf/global_config.cfg")
expects the resource to be available in relation to the class location. So, it will search as a relative path to the class inside the JAR. But the path /opt/xxx/conf/global_config.cfg is a absolute disk path, and for reading it , you need to use the FileInputStream
public Properties createProperties(){
Properties p = null;
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream =new FileInputStream("/opt/xxx/conf/global_config.cfg")) {
p = new Properties();
p.load(stream);
System.out.println(p.toString());
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
I will use user.properties to overwrite some properties in jmeter.properties.
Overwriting the properties summariser.out in jmeter.properties:
in jmeter.properties
summariser.out=true
in user.properties
summariser.out=false
In the apache doc is written:
Note: You can define additional JMeter properties in the file defined
by the JMeter property user.properties which has the default value
user.properties. The file will be automatically loaded if it is found
in the current directory or if it is found in the JMeter bin
directory. Similarly, system.properties is used to update system
properties.
so, my user.properties is in /bin and I the property in jmeter.properties -> user.properties=user.properties.
I tried also to load manually like:
Properties props = new Properties();
InputStream is = getTempInputStream(userPropTempFilePath);
props.load(is);
is.close();
That all has no effect.
Some idea how to load user.properties in java and to check if the properties are loaded?
Thats the solution:
String userProp = JMeterUtils.getPropDefault("user.properties", "");
if (userProp.length() > 0) {
FileInputStream fis = null;
try {
File file = JMeterUtils.findFile(userProp);
if (file.canRead()) {
log.info("Loading user properties from: "
+ file.getCanonicalPath());
fis = new FileInputStream(file);
Properties tmp = new Properties();
tmp.load(fis);
jmeterProps.putAll(tmp);
LoggingManager.setLoggingLevels(jmeterProps);//Do what would be done earlier
}
} catch (IOException e) {
log.warn("Error loading user property file: " + userProp, e);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
log.warn("There was problem closing file stream", ex);
}
}
}
Where is config.properties stored ?
I can't seem to track it down. I am able to read from it so I know it exists.
I use maven for dependency management, the WAR file is built using Eclipse default build action.
I checked all the following locations in the Navigator view, Package Explorer and the WAR file:
/
/src
/WebContent
/WebContent/Web-INF
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("os", OsDetect.getPropertyOsName());
// save properties to project root folder
prop.store(output, null);
}
catch (IOException io) {
io.printStackTrace();
}
finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I ended up putting the file in my WEB-INF/lib folder and manually edit it with the key/value properties. Placing it in the WEB-INF makes it unavailable for direct Servlet request.
To read the from the properties file I do the following :
private final String PROPERTIESPATH ="/WEB-INF/lib/config.properties";
private final Properties properties = new Properties();
private void loadProperties(ServletContextEvent context) {
....
InputStream input = context.getServletContext().getResourceAsStream(PROPERTIESPATH);
properties.load(input);
....
}
I have a file named InputFile.txt in a resources folder.
My project structure is like this:
VirtualMemory
src
resources
InputFile.txt
VirtualMemory
VirtualMemory.java
And I am trying to access the InputFile.txt in VirtualMemory.java class by like this:
String filename = ("./src/resources/InputFile.txt");
File file = new File(filename);
But the file is not being found. How to resolve this problem?
Below code will help load a properties file from any where in the classpath.
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
URL url = cl.getResource(CONF_PROPERTIES);
if (url == null) {
url = cl.getResource("/" + CONF_PROPERTIES);
}
if (url != null) {
try {
InputStream in = url.openStream();
props = new Properties();
props.load(in);
} catch (IOException e) {
// Log the exception
} finally {
// close opened resources
}
}
}