Not able to load properties file in Java - 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);

Related

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).

correct way to read properties file

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.

Write to properties file inside a java Package

How to write to properties file in a java package using java class in another package.
Here is the code for writing properties file
String filePath1 = "com/...../application.properties";
File applicationProperties = new File(filePath1);
FileOutputStream fileOutputStream = new FileOutputStream(applicationProperties);
Date todayDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
properties.setProperty("application.database.backup.date", sdf.format(todayDate));
properties.store(fileOutputStream, "storing index values to properties file");
fileOutputStream.close();
Getting FileNotFoundException.But file is exist in this package.while reading these file get the output.
String filePath = "com/....../application.properties";
InputStream inputStream = getClass().getResourceAsStream(filePath);
Properties properties = new Properties();
properties.load(inputStream);
if (properties.getProperty("application.grouping.mode") != null || !properties.getProperty("application.grouping.mode").isEmpty()) {
String lastBackupDate = properties.getProperty("application.grouping.mode");
}
How to solve this Exception.
There are three problems here, which are related. Basically, you're assuming that get because you can read from a resource, you can write to a file in the same folder structure, relative to the current directory. That's a flawed assumption because:
The resources may not be on the file system as separate files to start with. For example, Java applications are usually packaged up into jar files. The classloader knows how to read resources from a jar file, but the folder structure isn't present on disk
Even if the files are on disk as separate files in the right folder structure, they may not be rooted in the process's working directory. For example:
# Running in /home/jon/Documents
$ java -cp /home/jon/java/bin com.foo.bar.SomeApplication
Here SomeApplication.class would be in /home/jon/java/bin/com/foo/bar, but new File("com/foo/bar/something.properties") would refer to /home/jon/Documents/com/foo/bar/something.properties.
Finally, even if you were trying to write to the right place, you may not have write access - very often the binaries for applications are stored in read-only directories, with the reasonable justification that the code and read-only application data should be separated from the application's changing state. Aside from anything else, this makes updates/repairs much easier - just blow away the old version's directory, knowing that you won't have lost any user data.
Your context isn't clear, but I would suggest that you find some appropriate way of passing a filename to the application, and write to that and read from it where you need to.
Make sure your property file is in class path. after that you should be able to load it as
String filePath = "/application.properties";
InputStream inputStream = getClass().getResourceAsStream(filePath);
Your com is in the src directory. So your file path must be src/com/...... It must start with src/ and not com/

Java Spring - How to use classpath to specify a file location?

How can I use the classpath to specify the location of a file that is within my Spring project?
This is what I have currently:
FileReader fr = new FileReader("C:\\Users\\Corey\\Desktop\\storedProcedures.sql");
This is hardcoded to my Desktop. What I would like is to be able to use the path to the file that is in my project.
FileReader fr = new FileReader("/src/main/resources/storedProcedures.sql");
Any suggestions?
Are we talking about standard java.io.FileReader? Won't work, but it's not hard without it.
/src/main/resources maven directory contents are placed in the root of your CLASSPATH, so you can simply retrieve it using:
InputStream is = getClass().getResourceAsStream("/storedProcedures.sql");
If the result is not null (resource not found), feel free to wrap it in a reader:
Reader reader = new InputStreamReader(is);
From an answer of #NimChimpsky in similar question:
Resource resource = new ClassPathResource("storedProcedures.sql");
InputStream resourceInputStream = resource.getInputStream();
Using ClassPathResource and interface Resource. And make sure you are adding the resources directory correctly (adding /src/main/resources/ into the classpath).
Note that Resource have a method to get a java.io.File so you can also use:
Resource resource = new ClassPathResource("storedProcedures.sql");
FileReader fr = new FileReader(resource.getFile());
Spring has org.springframework.core.io.Resource which is designed for such situations. From context.xml you can pass classpath to the bean
<bean class="test.Test1">
<property name="path" value="classpath:/test/test1.xml" />
</bean>
and you get it in your bean as Resource:
public void setPath(Resource path) throws IOException {
File file = path.getFile();
System.out.println(file);
}
output
D:\workspace1\spring\target\test-classes\test\test1.xml
Now you can use it in new FileReader(file)
looks like you have maven project and so resources are in classpath by
go for
getClass().getResource("classpath:storedProcedures.sql")

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.

Categories