Reading Properties File in Java

Reading Properties file in Java

Based on your exception, the InputStream is null, this means the class loader is not finding your properties file. I'm guessing that myProp.properties is in the root of your project, if that's the case, you need a preceding slash:

InputStream stream = loader.getResourceAsStream("/myProp.properties");

How to read data from java properties file using Spring Boot

You can use @PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:

1.
Assign the property values to fields by using @Value with PropertySourcesPlaceholderConfigurer to resolve ${} in @Value:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

@Value("${gMapReportUrl}")
private String gMapReportUrl;

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}

}

2.
Get the property values by using Environment:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

@Autowired
private Environment env;

public void foo() {
env.getProperty("gMapReportUrl");
}

}

Hope this can help

Error in reading properties file in java

As the resources folder is a source folder, you can get an InputStream with:

input = QuartzTest.class.getResourceAsStream("/resources/config.properties");

correct way to read properties file

You could put your properties files into src/resources folder, and fetch them on classpath.

You could do something like this:

ResourceBundle bundle = ResourceBundle.getBundle("resources/consolemessages");

System.out.println(bundle.getString("INITIAL_MESSAGE"));

When using ResourceBundle, Locale support is also easy to implement, should you need to have language specific properties.

Read .properties file in java with newline

Your problem is, that you cannot put newline characters to XML attributes. Having \n in your properties file is perfectly right. But you transform this properties file to some proprietary XML format, or at least not to the properties XML format defined by the Properties class, because its format is different and there you could store newlines in values as they are not stored in attributes.

So whatever generates your XML, sets the value from the properties file as attribute value and during this the newlines are replaced by spaces which is expected. You have to escape the newlines, according to the XML format you use. If the software that reads this XML interprets \n in the attribute as newline character, then either your properts-to-XML converter has to escape the newline characters accordingly, or you have to esacpe the escape character in your properties file, thus having \\n which will cause to have \n as a two-character sequence instead of a newline character and thus it will end as \n in your XML attribute.

Property file reading error in spring boot

It doesn't work so. You have list with values under "outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory". It means you can add variable like so:

@Value("${outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory}")
private List<ParamWrapper> abc;

class ParamWrapper {

private String name;
private String impl;

....
getters and setters
}

And then check your params in this list.

Reading properties files inStream parameter is null

It's the declaration:

      <resource>
<directory>src/main/resources</directory>
...
<includes>
<include>*.png</include> <!-- just this one isn't enough -->
</includes>
</resource>

If you (re-)declare the resources explicitely it's not cummulative with the defaults, i.e. the defaults are overriden. Have a look at your target/classes and you will find just PNG images there.

See resources:[testR|r]esources:

Always uses the project.build.[testR|r]esources element to specify the resources to copy.

and POM Reference, Super POM:

<project>
...
<build>
...
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>

which means: Take all from these dirs by default but take nothing from these dirs if overridden (unless you include it when overriding).

Read properties file in java having particular string

Generally take a look on javadoc of java.util.Properties. To make you life easier I will give you this code snippet that can help you to start:

Properties props = new Properties();
props.load(new FileInputStream("myfile.properties"));
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
String value = props.getProperty(name);
// now you have name and value
if (name.startsWith("Appname")) {
// this is the app name. Write yor code here
}
}


Related Topics



Leave a reply



Submit