• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read properties file in netbeans?

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\ 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
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks and I have resolved it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You appear to have quoted a 1½ year-old thread and not asked a question yourself . . .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic