Strange exception: Properties File

Gurus,

I am trying to run the ff code:

if(properties ==null || properties.isEmpty()){

properties =new Properties();

File file =new File("settings/databasesetting.properties");

System.out.println("File path : "+file.getAbsolutePath());

try{

FileInputStream stream =new FileInputStream(file);

properties. load (stream);

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}//try

}//if

And I get the ff exception:

File path : C:\Development\Java\infosys\settings\databasesetting.properties

java.io.FileNotFoundException: settings\databasesetting.properties (The system cannot find the path specified)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream. (FileInputStream.java:106)

at com.web.database.Database. (Database.java:191)

at com.web.database.Database.main(Database.java:570)

Connection =null

java.lang.NullPointerException

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:164)

at com.web.database.DatabaseManager.getConnection(DatabaseManager.java:74)

at com.web.database.Database. (Database.java:200)

at com.web.database.Database.main(Database.java:570)

The strangest thing is that a file can be created using the given properties file, but I don't understand why an input strean cannot be created.

Please help, what am I doing wrong?

Thanks in advance.

Yours,

Me

[2235 byte] By [Khustaa] at [2007-10-3 3:42:23]
# 1
What does this have to do with Swing?> but I don't understand why an input strean cannot be createdAn InputStream is used to read existing files, not create them. You use an OutputStream to create a file.I don't understand your question.
camickra at 2007-7-14 21:38:19 > top of Java-index,Desktop,Core GUI APIs...
# 2
Creating a File object does not create a file on the filesystem. They are not the same thing.
itchyscratchya at 2007-7-14 21:38:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Creating a File object does not create a file on the

> filesystem. They are not the same thing.

Thank you guys for responding. But the problem I was posting for is the following:

I have a properties file that I want to load database properties from. But it gives me the problem mentioned above.. NB: The exception shown above does not give any indication about the argument you're making. The system just complains that the file cannot be found.

Now take a look at a response from a similar post I made at wwww.javalobby.org:

bviously your databasesetting.properties file is not located under the C:\Development\Java\infosys\settings folder.

That's why you get the FileNotFoundException when doing new FileInputStream(file).

You should not rely on absolute or relative path (relative to the current working directory) to load a file in Java.

It's better to rely on the class path (as allways in Java).

So you should:

1- Copy the databasesetting.properties file in a folder pointed by the class path.

For instance you could copy it under the same package folder than your class that tries to load it.

2- Then do instead of using the File and FileInputStream classes:

try {

InputStream stream = getClass().getResourceAsStream("databasesetting.properties");

properties.load(stream);

}

...

Again, thanks for taking time out and respond. I highly appreciate it!

Yours,

Me

Khustaa at 2007-7-14 21:38:19 > top of Java-index,Desktop,Core GUI APIs...
# 4
In fairness, the problem you were having was wholly unclear from your original post.Have you got it sorted now?
itchyscratchya at 2007-7-14 21:38:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

> In fairness, the problem you were having was wholly

> unclear from your original post.

>

> Have you got it sorted now?

Apologies for that oversight,

Yes, the problem has been solved partially. Using the approach suggested above, I has to move the properties file to the package of the class that reads the file. This is not a good solution, but I will look at better ways of solving it better later on.

Reason being I don't want to waste time on something that small.

Thanks again for your interest. In any forums you never learn about solutions to your problem BUT to ways to get the problem across as well.

Thanks again.

Yours,

Me

I

Khustaa at 2007-7-14 21:38:19 > top of Java-index,Desktop,Core GUI APIs...