correct way to read properties file - java

I am having trouble finding out the right way to load a properties file.
The structure is : Inside src/com.training , I have my class and the properties file as well. I have to read it using the absolute path as shown in the code below to get it to work:
Properties prop = new Properties();
InputStream input = null;
input = new FileInputStream("D:/Dev/workspace/Training/src/com/training/consolemessages.properties");
prop.load(input);
System.out.print(prop.getProperty("INITIAL_MESSAGE"));
How can I use the relative path to work in this code for accessing the properties file. The properties file and the class which is accessing are both at the same level '/src/com.training'

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.

Related

Spring include relative path in properties

I'm trying to make it so that in config/app.properties, I can have:
myfile.location=./myfile
where . is relative to said properties file. Is this possible ? I tried:
resourceLoader.getResource(appConfig.getMyFileLocation());
where resourceLoader and appConfig are autowired, but it won't work.
I usually reference my files like this:
myfile.location=classpath:myfile
where myfile is at the same location as the properties file.
Java properties are key-value pairs. So when you specify myfile.location=./myfile, this means appConfig.getMyFileLocation() will return './myfile' which is not the correct location.
As a workaround, you can get the location of the properties file and then use it along with the relative location to find the absolute path.
File propertyFileDirectory = .... // get the property file directory
String myfilePath = appConfig.getMyFileLocation();
File file = new File(propertyFileDirectory, myfilePath);

Not able to load the properties file into a java file

I am not able to load the properties file into a java file. getting file not found exception. Can you please help here.
Java file location:
classes/com/my/location/for/javabased/utilities/convertor/servlet/GetProp.java
Properties file location:
classes/com/my/property/properties/Config.properties
My code:
Properties inputParams = new Properties();
FileInputStream in = new FileInputStream("classes/com/my/property/properties/Config.properties");
inputParams.load(in);
in.close();
Getting File not found exception
I prefer reading the property file in class like this
public class SomeClass{
private static Properties someProperties = new Properties();
static{
someProperties.load(SomeClass.class.getResourceAsStream("/com/my/property/properties/Config.properties"));
}
Hope this helps.
The java class File (which is used by FileInputStream) is based on the file system path, either absolute, or relative to the current working directory. Both is mostly not under full control of the running application. If your resources can be found within the classpath (which from the point of view of the running application is always the same) you should use the resource loading mechanisme of your classloader (as Vijendra Kulhade pointed out in his answer).

Spring MVC Read and Parse a file in

I'm new to Spring and I'm just trying to open a simple text file and parse it so I can create some POJOs. My problem is I can't get Spring to find the txt file.
I've tried putting the file all over the place and right now I've put it in
/myAop/src/main/resources/myFile.txt
and I'm referencing it in a file called
com.myApp.pojo.Team.java
using code like this:
FileSystemResource resource = new FileSystemResource("/myApp/src/main/resources/myFile.txt");
File f =resource.getFile();
However, I'm not having any luck.
I've also tried several other locations.
Thanks in advance for the help.
You need to specify file name only as src/main/resource is already in classpath.
Try this:
FileSystemResource resource = new FileSystemResource("myFile.txt");
File f =resource.getFile();
Try
New Xmlclasspathresource() .
Try anothe path
FileSystemResource resource = new FileSystemResource("/main/resources/myFile.txt");
Actually the file from resources is placed under classes.
Try using absolute path
FileSystemResource resource = new FileSystemResource("file:C:/test/workspace/src/myApp/src/main/resources/myFile.txt");
You may use classpath as well, but the file should be in the classpath.
FileSystemResource resource = new FileSystemResource("classpath:myApp/src/main/resources/myFile.txt");
What I wound up doing was putting the file in base directory with the POM.xml file and then i could get to it just by using the name and not by having to use any kind of path. I'm sure there are better ways but for the time being I just needed to move on.

How to call database.properties outside the project using notepad in java?

I want to take place database.properties outside the project, so when I want to change the content (database configuration) of that when I've build them into jar, I can do it easily without open my project again. So what to do?
First, place the database.properties file in the location you'd like it to be in.
Then, do one of the following:
Add the directory where database.properties is located, to the classpath. Then use Thread.currentThread().getContextClassLoader().getResource() to get a URL to the file, or getResourceAsStream() to get an input stream for the file.
If you don't mind your Java application knowing the exact location of the database.properties file, you can use simple File I/O to obtain a reference to the file (use new File(filename)).
Usually, you'd want to stick with the first option. Place the file anywhere, and add the directory to the classpath. That way, your Java application doesn't have to be aware of the exact location of the file - it will find it as long as the file's directory is added to the runtime classpath.
Example (for the first approach):
public static void main(String []args) throws Exception {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
Properties props = new Properties();
try {
// Read the properties.
props.load(stream);
} finally {
// Don't forget to close the stream, whatever happens.
stream.close();
}
// When reaching this point, 'props' has your database properties.
}
Store properties file in your preferred location. Then do the following:
try {
String myPropertiesFilePath = "D:\\configuration.properties"; // path to your properties file
File myPropFile = new File(myPropertiesFilePath); // open the file
Properties theConfiguration = new Properties();
theConfiguration.load(new FileInputStream(myPropFile)); // load the properties
catch (Exception e) {
}
Now you can easily get properties as String from the file:
String datasourceContext = theConfiguration.getString("demo.datasource.context", "jdbc/demo-DS"); // second one is the default value, in case there is no property defined in the file
Your configuration.properties file might look something like this:
demo.datasource.context=jdbc/demo-DS
demo.datasource.password=123

Not able to load properties file in Java

I am trying to load a properties file. The properites file is in the class path of the application.
Properties p = new Properties();
p.load(new FileInputStream("classpath:mail.properties"));
System.out.println(p.get("hi"));
Now I say classpath, because another file called x.properties is referred in an xml file like this
<property name="x">
<util:properties location="classpath:x.properties" />
</property>
I placed my mail.properties in the same folder as x.properties, but my Java program is not able to find it ? Any idea what I am missing ?
Just because some program processing that XML file likes the syntax classpath:x.properties doesn't mean that it is a universally accepted syntax in Java!
If you provide "classpath:x.properties" to a FileInputStream it will look for a file named classpath:x.properties. (Check the documentation of that particular constructor.)
Try providing the full path to that file. If the file happens to be on your class path, you could use something like
p.load(getClass().getResourceAsStream("mail.properties"));
if mail.properties is indeed on your classpath, you will have better luck loading it via a class loader:
Properties p = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("mail.properties");
p.load(is);

Categories