Hi all,

I want to read a properties file from the same directory as my program, e.g. my program is in C:\WorkSpage\Myprogram.java and I have a properties file called index.properties how can I read the file, without specifying a path?

I have tried the following but it doesn't read the file:

private static final String FILE_NAME = "objectid.properties";
File f = new File(FILEP_NAME);
if(f.exists()){
    Properties pro = new Properties();
    FileInputStream in = new FileInputStream(f);
    pro.load(in);
    // rest of code that handle the values from file....
}

But if I specify my FILE_NAME value as "C:\WorkSpage\index.properties" and give it an absolut path, then it works.

Can someone please advise...


Thanks.

if this is exact code as you use then you have typo

private static final String FILE_NAME = "objectid.properties";
File f = new File(FILEP_NAME);

otherwise I do not see any other problem with your code.

Well, you are showing two different file names there, so that might be a problem. If you are having difficulties with specifying a relative file path, make sure that path resolves correctly against the current user directory, which can by checked with System.getProperty("user.dir").

if this is exact code as you use then you have typo

private static final String FILE_NAME = "objectid.properties";
File f = new File(FILEP_NAME);

otherwise I do not see any other problem with your code.

I did a typo error when I posted my message, in the code it is actually correct name FILE_NAME....

Can someone please suggest...


Thanks

Can someone please suggest...

Yes, and I did.

Yes, and I did.

Thanks man, that helped :-)

In eclipse, the user directory is infact not the same directory where your source file is, it treats the work space as user current directory...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.