Loading Java Properties Files by Profile - java

I want to load Properties Files in Java code.
But I use profile to config -Dspring.profiles.active=local or dev...
How to load properties files by profile Something like this:
classpath:${spring.profiles.active}/test.properties
How to do that in Java code ?
I did as below, but get null.
Properties prop = new Properties();
InputStream iStream = Helper.class.getClassLoader().getResourceAsStream("test.properties");
try {
prop.load(iStream);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
try {
iStream.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}

This is some working code for us:
String activeProfile = System.getProperty("spring.profiles.active");
InputStream workSpacesFIS = this.getClass().getClassLoader()
.getResourceAsStream(activeProfile + "/customers.txt");
if (workSpacesFIS != null) { ...

Loading Java Properties Files by Profile
public Properties getProp() throws IOException {
final Properties prop = new Properties();
prop.load(TestService.class.getResourceAsStream("/application.properties"));
String activeProfile = prop.getProperty("spring.profiles.active");
prop.load(TestService.class.getResourceAsStream("/application-"+activeProfile+".properties"));
return prop;
}

Related

How to save data from a Properties object to a File + How to load the properties-formatted file to a Properties object in another Method?

I want to save the data from the properties objecet config to the file configFile in the parameter like following:
#Override
public void saveConfig(Properties config, File configFile) {
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(configFile));
os.writeObject(config);
os.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
In the next method I want to load the properties-formatted configFile to a Properties object and return it:
#Override
public Properties loadConfig(File configFile) {
Properties prop = new Properties();
try(InputStream input = new FileInputStream(configFile)){
prop.load(input);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return prop;
}
Somehow the JUnit test is showing me a NullPointerExeption (note: it is an exam)
if (!config.getProperty("testKey").equals("testValue"))
fail("sample config data doesn't match read config data!");
What am I missing here?
The following examples use the java.nio.file package which should be preferred to java.io.File due to its improved error handling. However, the code will look similarly for java.io.File as well.
Writing Properties
#Override
public void saveConfig(Properties config, Path configFile) throws IOException {
// Comments to be written at the beginning of the file;
// `null` for no comments
String comments = ...
// try-with-resources to close writer as soon as writing finished
// java.nio.file.Files.newBufferedWriter​(...) uses UTF-8 by default
try (Writer writer = Files.newBufferedWriter(configFile)) {
config.store(writer, comments);
}
}
Reading Properties
#Override
public Properties loadConfig(Path configFile) throws IOException {
Properties config = new Properties();
// try-with-resources to close reader as soon as reading finished
// java.nio.file.Files.newBufferedReader(...) uses UTF-8 by default
try (Reader reader = Files.newBufferedReader(configFile)) {
config.load(reader);
}
return config;
}

How to get file jpg properties in Android Studio

Firstly I get an ArrayList using the method getFilePaths.
method { List out all images from SD card.}
How to continue?
At phone, when you see details over image, you can see:
tittle, hour, width, height, orientation, fileSize, path...
I want get all attributes/details/properties of a file jpg and save them in variables.
I tried do this: Properties Class Java but I think that's not the right way
You can retrieve Properties from File like below code add your file into below code and get Properties object
Properties prop = new Properties();
// load a properties file
prop.load(input);
private void retrievePropertiesFromFile(){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/CHETAN");
String fname = "mytext.txt";
File myFile = new File (myDir, fname);
InputStream input = null;
try {
input = new FileInputStream(myFile);
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
Log.i(getClass().getSimpleName(),prop.getProperty("text"));
Log.i(getClass().getSimpleName(),prop.getProperty("textstyle"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Ok, I using this simple code and it show null in property text:
File imageJPG = new File("/storage/emulated/0/WhatsApp/Media/WhatsApp Images","6gMRtQyY.jpg");
if(imageJPG.isFile()){
System.out.println("its file");
System.out.println("image: "+imageJPG);
}else{
System.out.println("no");
}
InputStream input = null;
try {
input = new FileInputStream(imageJPG);
Properties prop = new Properties();
prop.load(input);
// get the property value and print it out
System.out.println("property text: "+prop.getProperty("text"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Console:
I/System.out: its file
I/System.out: image: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/6gMRtQyY.jpg
I/System.out: property text: null

FileInputStream fails on properties file

This properties file in PROJECT/resources/properties.properties can be read and show its content with:
public void showFileContent(String fileName){
File file = new File (fileName);
FileInputStream input = null;
if(file.exists()){
int content;
try {
input = new FileInputStream(fileName);
while ((content = input.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
}
But it fails with a null pointer exception at properties.load with that code
public Properties getProperties(String fileName, Properties properties){
File file = new File (fileName);
InputStream input = null;
if(file.exists()){
try {
input = new FileInputStream(fileName);
properties.load(input);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
return properties;
}
even when input is set to
input = this.getClass().getClassLoader().getResourceAsStream(fileName)
anyone knows why that can be for a properties text file at the same path for both methods ?
Since the first code snippet works, it seems properties is passed as null to the getProperties() method, resulting in NullPointerException.
Ideally, we shouldn't be passing the properties at all. We just need to create a new object and return it.

Don't find my file.properties

I try a lot of thinks to find the fail but i don't know how I can do it. my code is:
//DominioLlamadaRedSys.java
Properties d = new Properties();
InputStream entrada = null;
try {
entrada = new FileInputStream("prop/datosApp.properties");
d.load(entrada);
System.out.println(d.getProperty("TXD.endPointUrl"));
} catch (IOException ex) {
System.out.println("ERROR: "+ ex.getMessage());
} finally {
if (entrada != null) {
try {
entrada.close();
} catch (IOException e) {
}
}
}
I call the file inside a class in "com.rsi.secpay.dominio" and this always catch the same exception (don't find the file), I had try to quit "prop/" (just "datosApp.properties" ) with properties files like this:
If your prop package is in your classpath, you can get the stream using the classloader:
InputStream is = DominioLlamadaRedSys.class.getResourceAsStream("/prop/datosApp.properties");

reading and storing properties file

I am trying to update a value in property file through the following code
import java.io.*;
import java.util.Properties;
public class Sample {
public static void main(String a[]) throws IOException {
InputStream is = Sample.class.getClassLoader().getResourceAsStream("myfile.properties");
Properties p = new Properties();
p.load(is);
p.setProperty("myProperty", "updated");
OutputStream os = new FileOutputStream("myfile.properties");
p.store(os, "update");
os.close();
System.out.print(p.getProperty("myProperty"));
}
}
Output: updated
But the values doesn't seem to get updated. In Fact, I am not getting any error even if the property or the file itself is not present.
// Read properties file.
Properties prop = new Properties();
try {
prop.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}
// Write properties file.
try {
prop.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}

Categories

Resources