Click to See Complete Forum and Search --> : reading in a properties file
annie613
11-25-2005, 08:32 AM
hi all -
i am running into a problem
my application runs fine as is but
what i am trying to do is use a .properties file to read in data instead of having it in my main method
im using this block of code to call the .properties file
String propertiesFileName = "unload.properties";
ClassLoader cl = Unload.class.getClassLoader();
InputStream in = cl.getResourceAsStream(propertiesFileName);
Properties prop = new Properties();
prop.load(in);
but when i comment out my vars in my code and run the code it tells me
java.lang.NullPointerException
at java.io.File.<init>(File.java:194)
at Unload.main(Unload.java:60)
Exception in thread "main"
i thought i had it set correctly but i guess i dont
any suggestoins - thanks in advance annie :)
Khalid Ali
11-26-2005, 12:21 PM
its a bit ambiguous that what u comment out, could u please elaborate?...in my opinion if u access a file object that is not initialized with a valid file it will be null
annie613
11-28-2005, 07:30 AM
public class Unload
{
private static String USERID = null; //"userid123";
private static String PWD = null; //"abc123";
private static String SERVER = null; //"DEV_DB";
private static String COM_IBM_DB2_JDBC_APP_DB2DRIVER = null; //"COM.ibm.db2.jdbc.app.DB2Driver";
private static String JDBC_DB2 = null; //"jdbc:db2:";
private static String HOSTNAME = null; //"myserver";
public static void main(String args[])
{
try
{
String propertiesFileName = "\\unload.properties";
ClassLoader cl = Unload.class.getClassLoader();
InputStream in = cl.getResourceAsStream(propertiesFileName);
Properties prop = new Properties();
prop.load(in);
String folderName = null; //"\\C:\\unload"; //folder to read from
String processedFolderName = null; //"\\C:\\unload_processed"; //folder to transfer to
File f = new File(folderName);
File files[] = f.listFiles();
while(true)
{
try
{
//make database connection
Connection conn;
Class.forName(COM_IBM_DB2_JDBC_APP_DB2DRIVER).newInstance();
conn = DriverManager.getConnection(JDBC_DB2 + SERVER, USERID, PWD);
for(int i = 0; i < files.length; i++)
{
if(files[i].isFile())
{
System.out.println(files[i]); //print the file that was read
BufferedReader inStream = new BufferedReader(new FileReader(files[i]));
String line = inStream.readLine();
.
.
.
}
on this line
prop.load(in);
i get this error
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:104)
at java.io.InputStreamReader.<init>(InputStreamReader.java:93)
at java.util.Properties.load(Properties.java:214)
at Unload.main(Unload.java:55)
Exception in thread "main"
but when i uncomment and run it with out this code block
String propertiesFileName = "\\unload.properties";
ClassLoader cl = Unload.class.getClassLoader();
InputStream in = cl.getResourceAsStream(propertiesFileName);
Properties prop = new Properties();
prop.load(in);
it runs?!??!?!
i am not sure how to set up my properties file or where to put it to have the appliation read it correctly.
this is what my .properties file looks like
#DB USERID
USREID=userid123
#DB PASSWORD
PWD=abc123
#DATABASE
SERVER=DEV_DB
#DRIVER
COM_IBM_DB2_JDBC_APP_DB2DRIVER=COM.ibm.db2.jdbc.app.DB2Driver
#JDBC
JDBC_DB2=jdbc:db2:
#HOSTNAME
HOSTNAME=myserver
#FOLDER TO READ FROM
folderName=\\C:\\unload
#FOLDER TO XFR TO
processedFolderName=\\C:\\unload_processed
# End of file
any suggestions?? thanks again! :) annie
Khalid Ali
11-28-2005, 01:14 PM
Hi Annie,
The following code will read a properties file and will print out name and value pairs.
try {
ClassLoader cl = Tests.class.getClassLoader();
Properties props = new Properties();
props.load(cl.getResourceAsStream("unload.properties"));
Enumeration enum = props.keys();
while(enum.hasMoreElements()){
String name = enum.nextElement().toString();
System.out.println("Name["+name+"] value["+props.getProperty(name)+"]");
}
} catch (IOException e) {
e.printStackTrace();
}
The most important thing here is that unload properties file should be where the compiled class files are..for e.g if you have a classes folder where all of the compiled code is, then this properties file should be in there.
When I tested the above code ( I created an unload.properties file) and put the values that u have in the last post, I get the following output.
Name[HOSTNAME] value[myserver]
Name[COM_IBM_DB2_JDBC_APP_DB2DRIVER] value[COM.ibm.db2.jdbc.app.DB2Driver]
Name[USREID] value[userid123]
Name[processedFolderName] value[\C:\unload_processed]
Name[JDBC_DB2] value[jdbc:db2:]
Name[PWD] value[abc123]
Name[SERVER] value[DEV_DB]
Name[folderName] value[\C:\unload]
annie613
11-29-2005, 08:48 AM
i am using the code posted above and it seems to print out the name and values in my properties file.
but i thought i had to set them this way in my app as well by adding this additional code
USERID = props.getProperty(USERID.toString());
PWD = props.getProperty(PWD.toString());
SERVER = props.getProperty(SERVER.toString());
COM_IBM_DB2_JDBC_APP_DB2DRIVER = props.getProperty(COM_IBM_DB2_JDBC_APP_DB2DRIVER.toString());
JDBC_DB2 = props.getProperty(JDBC_DB2.toString());
HOSTNAME = props.getProperty(HOSTNAME.toString());
so that way my vars have data.
when i run it with or without this block of code i sitll get a NullPointerException.
i see that the props are being loaded but they are still null????
im not sure why i can read the name and values from the file but cant use the content??? any suggestions??? thanks again cheers
annie613
11-29-2005, 11:33 AM
USERID = props.getProperty("USERID");
PWD = props.getProperty("PWD");
SERVER = props.getProperty("SERVER");
JDBC_DB2 = props.getProperty("JDBC_DB2");
HOSTNAME = props.getProperty("HOSTNAME");
COM_IBM_DB2_JDBC_APP_DB2DRIVER = props.getProperty("COM_IBM_DB2_JDBC_APP_DB2DRIVER");
i think this is all i needed :) CHEERS and thanks a million! :)