probelm in properties

Hi,

I am trying to read from a property file using the following code. I dont get the value for "name" key. So I tried to read the number of keys in the file, that too return 0. Could you please help me.

Code:

-

static String rateFinder()

{

int elen=0;

Properties props=new Properties();

try

{

props.load(new FileInputStream(new File("test.properties")));

}

catch(IOException ie)

{

System.out.println("Error reading file");

}

// String key = "name";

// String val = props.getProperty(key,"notfound");

Enumerationenum = props.keys();

while(enum.hasMoreElements())

{

elen+=1;

}

return Integer.toString(elen);

}

test.properties

-

name=subi

roll=101

mark1=100

*****

Both the files are in same location, indeed i do not get IOException. It seems the method reads the file but not the contents.

I have to use static method as i read the returned value in a method which should take only static values.

Pls help.

Thanks,

subi.

[1790 byte] By [subi_phoenixa] at [2007-10-2 8:41:54]
# 1

Enumeration enum = props.keys();

while(enum.hasMoreElements())

{

elen+=1;

}

enum is a keyword in version 1.5

it's adviseable not to use this as a variable name.

in your loop you never call next()

why not just test it using the list(PrintStream out) method?

kind regards

walken

walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 2

looks like it's a problem with your properties file.

suggest you print out the entire path like

try {

File f = new File("test.properties");

System.out.println("absolute path to property file: "+f.getAbsolutePath());

props.load( new FileInputStream(f));

}

that way, if you are using an IDE, you will know where it expects to find the property file

kind regards

walken

walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 3
in other words,if you are running this within an IDE then it might expect to find a file in a different location than you think it's really looking and if it doesn't find it then you won't get any keys/valueskind regardswalken
walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 4

> in other words,

> if you are running this within an IDE then it might

> expect to find a file in a different location than

> you think it's really looking and if it doesn't find

> it then you won't get any keys/values

Then he either has an empty, duplicate file, or the FileInputStream c'tor would barf.

CeciNEstPasUnProgrammeura at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 5
> Then he either has an empty, duplicate file, or the> FileInputStream c'tor would barf.indeed you are right (of course)the OP did say there was no exception so scratch that.kind regardswalken
walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 6

Thank you Walken. I used the below code as you suggested and I got the exception raised. But I have both the files(unique) in same location and havecontents in property file.

String strPath="";

try

{

File f = new File("test.properties");

props.load( new FileInputStream(f));

strPath=f.getAbsolutePath();

}

catch(IOException ie)

{

strPath="Error reading file";

}

return strPath;

What went wrong here?

Thank you,

Subi

subi_phoenixa at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 7
> What went wrong here?You didn't print the stack trace. Apart from that packages might have gotten into your way.
CeciNEstPasUnProgrammeura at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 8

String s=" ";

try {

File f = new File("test.properties");

props.load( new FileInputStream(f));

//Enumeration enum=props.keys();

s=props.getProperty("name");

}

catch (IOException e) {

e.printStackTrace();

}

return s;

check out whether this wrks....when i tried ur previous code i too got exception...but this wrks...

My_screena at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 9
Thank you CeciNEstPasUnProgrammeur.I got the Following error message as i used ie.getMessage():test.properties (The system cannot find the file specified)But Im have both in same folder. Do i need to incorporate different idea?Thanks,subi
subi_phoenixa at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 10
> check out whether this wrks....when i tried ur> previous code i too got exception...but this wrks...the code originally posted works for me provided a valid property file is found.kind regardswalken
walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 11

File f = new File("test.properties");

System.out.println(f..getAbsolutePath());//print the path here

print the path before the exception is thrown and see if it's what you expect.

kind regards

walken

walken16a at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 12

> But Im have both in same folder.

Are you using any packages? And you're trying to read the file from the present working directory. Which has nothing to do with the location of your program. Especially not the location of your source code.

And why isn't this a ResourceBundle anyway?

CeciNEstPasUnProgrammeura at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 13

Thanks My_Screen and All,

I tried usign this also but getting same exception. "test.properties (The system cannot find the file specified)". Is there any other way to specify file location?

I could not use getServletConfig().getServletContext().getRealPath("\test.properties")

as it raises error saying it can not be used in static method. But I have to use static method as I have said earlier.

Please help.

Thanks,

subi

subi_phoenixa at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 14
You are not really supposed to use direct file system access in J2EE. Especially not for resources. Use ResourceBundle.
CeciNEstPasUnProgrammeura at 2007-7-16 22:44:02 > top of Java-index,Java Essentials,Java Programming...
# 15
Yes. Im using packages. Currently, source code, class and properties files are residing in same folder called titan which is also a package.
subi_phoenixa at 2007-7-20 19:50:23 > top of Java-index,Java Essentials,Java Programming...
# 16
> getServletConfig().getServletContext().getRealPa> th("\test.properties") that's funny,until now, i completely missed the part about this being web based app.kindly enlightenedwalken
walken16a at 2007-7-20 19:50:23 > top of Java-index,Java Essentials,Java Programming...
# 17

> Yes. Im using packages. Currently, source code, class

> and properties files are residing in same folder

> called titan which is also a package.

So the resource is not in the same folder as your class.

Also, your present working directory is unknown and not likely to be where your program resides.

And you should not ******* use file paths to load a resource in an J2EE environment. Go ahead, pack it into a web app or distribute it using JNLP and see it break again.

CeciNEstPasUnProgrammeura at 2007-7-20 19:50:23 > top of Java-index,Java Essentials,Java Programming...
# 18

you are right walken, I put the f.getAbsolutePath() before loading the property file.

And shockingly the system looks for the file in "C:\WINNT\system32\test.properties" while the actual file location is "c:\websphere\appserver\hosts\default_host\titanapp\servlets\titan\test.properties"

so, can I specifiy the full path like File f= new File ("c:\websphere\appserver\hosts\default_host\titanapp\servlets\titan\test.properties");

and then load property file?

Thanks,

subi

subi_phoenixa at 2007-7-20 19:50:24 > top of Java-index,Java Essentials,Java Programming...
# 19

> you are right walken, I put the f.getAbsolutePath()

> before loading the property file.

>

> And shockingly the system looks for the file in

> "C:\WINNT\system32\test.properties" while the actual

> file location is

> "c:\websphere\appserver\hosts\default_host\titanapp\se

> rvlets\titan\test.properties"

>

> so, can I specifiy the full path like File f=

> new File

> ("c:\websphere\appserver\hosts\default_host\titanapp\s

> ervlets\titan\test.properties");

> and then load property file?

>

>

> Thanks,

> subi

no, better look a Ceci's last post.

I didn't realize it was a web app.

kind regards

walken

walken16a at 2007-7-20 19:50:24 > top of Java-index,Java Essentials,Java Programming...
# 20

> And shockingly the system looks for the file in

> "C:\WINNT\system32\test.properties" while the actual

> file location is

> "c:\websphere\appserver\hosts\default_host\titanapp\se

> rvlets\titan\test.properties"

Not shocking at all. It's what I told you all the time.

> so, can I specifiy the full path like File f=

> new File

> ("c:\websphere\appserver\hosts\default_host\titanapp\s

> ervlets\titan\test.properties");

> and then load property file?

Yes, you can. And as soon as you deploy it somewhere else, it breaks again.

CeciNEstPasUnProgrammeura at 2007-7-20 19:50:24 > top of Java-index,Java Essentials,Java Programming...
# 21

ha...ha...ok no more shocks.. :) Thanks for your suggestion.

so what should I use now? to avoid that break. I tried with following code:

String getPath()

{

String propFile=getServletConfig().getServletContext().getRealPath("\test.properties");

return propFile;

}

static String rateFinder()

{

Properties props=new Properties();

String hello=new TitanGeneral().getPath();

return hello;

}

I create instance of the class because of I could not use the getServletConfig() in the static method.

But I got NullPointerException.

subi_phoenixa at 2007-7-20 19:50:24 > top of Java-index,Java Essentials,Java Programming...