How to open a properties file from another package on Netbeans (JAVA) - java

I have a bean that needs to take some parameters from a properties file but I can not find it (java.lang.NullPointerException) in order to open it. My bean is in the extra.beans package while the properties file is in the extra.dao package. I am trying to do
file = new FileInputStream("database.properties");
prop.load(file);
and I have tried any possible combination for the path but I can not find it. I am using Netbeans 7.4. How can I open it?

You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username

Specify the full path. It should work.

If you're loading the properties file into a Properties object, try the something like:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));
I don't know your full package structure but using this approach and putting the full path to the properties file would also work, ie: /extra/dao/database.properties

file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);

Related

How to setup custom serenity.properties location?

I would like to load custom serenity.properties from specific directory etc. src/test/resources/properties/serenity.properties via gradle
I tried to use -Dproperties parameter with absolute path to file (as it is recommended on their site) but it was not working
You can put your Properties werever you would like but you need to remember the properties is part of the project. Therefore when you pass in your -d properties parameter it will be from the directory you are running the project from. So you would do something very simple like:
-Dproperties=src/test/resources/properties/serenity.properties
Mine is in a properties directory on the root and I modified the name so mine looks like this:
-Dproperties=properties/desktop.properties
You can create a method in your build which loads the data from properties file.
// Read serenity.properties
def getSerenityProperties(){
def props = new Properties();
def propFile = file('/path/to/directory/with/serenity.properties')
props.load(propFile.newDataInputStream())
return props;
}
// assign properties data to a variable
project.ext.serenity = getSerenityProperties()
// use property
serenity.xyz
serenity.pqr

How to read properties file inside jar?

I have a config.properties file under the package com.abc.properties. From one of the java class present in com.abc.util, I need to read the property file. Both the files are present inside jar.
I have tried using
fs = new FileInputstream(VerifyFolderStructure.class.getResourceAsStream("com/abc/properties/config.properties"));
But it doesn't seem to work. Please help.
P.S: VerifyFolderStructure is my java class from which I need to load the properties file.
To obtain the stream you don't need FileInputStream at all. You can get the properties' file stream like this
InputStream is = VerifyFolderStructure.class.getResourceAsStream("/com/abc/properties/config.properties");
I think you need to add "/" in the front of you path.if you don't add "/" that means the properties is located in the same packages path as VerifyFolderStructure class.

Is there any way to change a properties file that is in my classpath?

I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?
One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?
It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.
Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the store and load methods in that class. Mind that if you use the versions that take an Output/InputStream, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriate Writer/Reader.
Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.
Here is a sample code:
Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");
OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");
You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.
Below is an example:
Properties pproperties = new Properties();
if (properties.containsKey("key1")) {
properties.remove("key1");
properties.setProperty("key1", "value1");
properties.store(new FileOutputStream("file.properties"), null);
}

how to search for string in file

How could I search for string in text file in java?
Would it have to be in a text file or could it read a .ini or some other file type
example
sound = off
opengl = on
openal = off
I tried searching for it on google but I couldnt really find anything.
Thanks in advance
It looks like you want to use a Properties file.
You can load one using Properties.load(String path);
If your file is a properties file or an ini file (key = value), you may want to use the Apache Commons Configuration API. It is much better than the Properties class from the JDK.
There are tons of information with those typical of questions.
Here you have two easy examples:
Loading Java Properties Files http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
How do I load property settings with the Properties class? http://www.jguru.com/faq/view.jsp?EID=581608
In short it is easy to load a file into a Properties object, for example to obtain, in your case, the sound value in a example.properties file:
Properties props = new Properties();
props.load(new FileInputStream("example.properties"));
String isOnOrOff = props.getProperty("sound");

How to load properties file in Google App Engine?

So I'm trying to add some ability to my project to allow user-defined properties in my deployment artifact - a simple key:value .properties file. I place the service.properties file in
war/WEB-INF/my-service.properties
And in my ServiceImpl.java constructor I have the following:
String propertiesFileName = "my-service.properties";
URL propertyURL = ClassLoader.getSystemResource(propertiesFileName);
URL propertyURL2 = this.getClass().getClassLoader().getResource(propertiesFileName);
URL propertyURL3 = this.getClass().getClassLoader().getResource( "WEB-INF/" + propertiesFileName);
URL propertyURL6 = this.getClass().getClassLoader().getResource(
"E:/Projects/eclipse-workspace/projectName/war/WEB-INF/" + propertiesFileName);
All instances of Property URL are null. I know I'm missing something absolutely obvious, but I need a second pair of eyes. Regards.
EDIT:
Ah, it seems I was confused as the default GAE project creates a logging.properties file in /war. From the Google App Engine documentation:
The App Engine Java SDK includes a template logging.properties file, in the appengine-java-sdk/config/user/ directory. To use it, copy the file to your WEB-INF/classes directory (or elsewhere in the WAR), then the system property java.util.logging.config.file to "WEB-INF/classes/logging.properties" (or whichever path you choose, relative to the application root). You can set system properties in the appengine-web.xml file, as follows:
Try putting the service.properties in WEB-INF/classes. Then it should be accessible just with :
this.getClass().getClassLoader().getResourceAsStream("/filename.properties");
As Mike mentioned in his comment to jsights answer, it worked for me if I used
this.getClass().getClassLoader().getResourceAsStream("filename.properties");
(removed the first slash) after placing the file in WEB-INF/classes.
I think what you will need is something like this:
String filePath = servletContext.getRealPath("/WEB-INF/views/") + "/" + mav.getViewName() + ".vm";
FileInputStream in = new FileInputStream(filePath);
I get the servletContext from spring: #Autowire ServletContext.

Categories

Resources