This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes property files (Why does java make this so hard?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "property files (Why does java make this so hard?)" Watch "property files (Why does java make this so hard?)" New topic
Author

property files (Why does java make this so hard?)

Sheldon Plankton
Ranch Hand

Joined: Feb 11, 2004
Posts: 33
Hi,
All I want to be able to is avoid having to hard code some values in my code.
So I assumed that I should stote this information in a "properties" file, right? (Maybe this is my mistake) Well anyways please take a looks at this ...
bash-2.03$ cat my.properties
property_1 value1 value2 value3 value4
bash-2.03$ cat MyProps.java
import java.io.*;
import java.util.*;

public class MyProps {
public static void main(String[] args) {
Properties myProps = new Properties();
String propFileName = "my.properties";
InputStream is =
Turn_The_File_Associated_With_The_Filename_Into_A_InputStream
( propFileName );
myProps.load( is );
System.out.println( "props = [" + myProps + "]" );
}
}
... let me know what I am suppose to do.
Thanks
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
The secret is in the classpath. Put the .properties file in your classpath, just like your .class file(s) are. To open the input stream:
InputStream propStream = getClass().getResourceAsStream("/yourFile.properties");
 
Consider Paul's rocket mass heater.
 
subject: property files (Why does java make this so hard?)
 
Similar Threads
Doesn't java.util.regex come with J2EE 1.4 SDK?
How do I package stuff?
Cannot create InputStream from Properties file
Resource file loction
Invalid directive? outdated directive?