Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sheldon Plankton
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic