File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to read properties file in netbeans? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read properties file in netbeans?" Watch "How to read properties file in netbeans?" New topic
Author

How to read properties file in netbeans?

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I have created properties file in netbeand under the same package as my source file. In the same directory as Project/sourcePacakages/package.. Both my java and properties file are under package. But I am not able to open and read my properties file from my java source program. Can any one tell why it is not able to open the config file. Thanks.



And my properties file is as below:


[ April 15, 2008: Message edited by: Gopu Akraju ]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
If I give complete path of the file, it works. How to overcome this?

My second problem is I modified the properties file as below so that I can open file through java

But when I get the properties as below


I get the string as outDir = "C:\xyz\testing"; which is throwing error when I use it for file processing. Why the path \\ gets into \? How to fix this? Thanks.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

It's because the path is wrong. If you put the property file like this
properties.load(new FileInputStream("Config.properties"));
That actually means the location of the properties file is in
System.getProperty("user.dir")/Config.properties, which is not the place where you put your properties file because you put the Config.properties in the src directory.

Probably the best way is to either specify the property file path in the System property or you can also put it in the classpath and scan for that file.

If you want to have a quick hack to see if your program can read the properties file, create the Config.properties in your project directory. Go to the Files tab and create it in the same level as build.xml.

Hope this helps


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Just one more thing to add. If you run your application from NetBeans, the "user.dir" is the path of your NetBeans project.

Try to do System.out.println(System.getProperty("user.dir")) and see where it is. If you specify the relative path, e.g. props.load(new FileInputStream("Config.properties")), that's basically equivalent to
System.getProperty("user.dir") + File.separator + "Config.properties"
If System.getProperty("user.dir") + File.separator + "Config.properties" is the location of your properties file, then I'm sure your program should be able to load it; otherwise you'll get an exception.
[ April 15, 2008: Message edited by: Freddy Wong ]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks Fred, both works fine now and I prefer to put the properties file under project and work as I have to deply the application on some other m achine. Regarding my second question. My properties like is as below now.

because in my java program I want my string as C:\\xyz\\testing for further file processing. But now when I use teh method
, the tsring is like C:\xyz\testing, hence throwing error in file operations. Why \\ is getting reset to \. I need to resolve this so that I can use the path for file processing. Thanks.
[ April 15, 2008: Message edited by: Gopu Akraju ]
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

\ is an escape character, basically it has a special meaning to the compiler. If you use \\, it will be treated as \. Thus, when you print you will only get \. If you want to get \\, you need to do \\\\.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks and I have resolved it.
Kaasi reddy
Greenhorn

Joined: Oct 20, 2009
Posts: 1
Gopu Akraju wrote:I have created properties file in netbeand under the same package as my source file. In the same directory as Project/sourcePacakages/package.. Both my java and properties file are under package. But I am not able to open and read my properties file from my java source program. Can any one tell why it is not able to open the config file. Thanks.



And my properties file is as below:


[ April 15, 2008: Message edited by: Gopu Akraju ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Welcome to JavaRanch

You appear to have quoted a 1½ year-old thread and not asked a question yourself . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read properties file in netbeans?
 
Similar Threads
properties file
write into a file?
File Becomes Locked so App Cannot Make Changes to it
Lost properties
Loading Properties file