-1

I created the properties file, say config.properties. Now I would like to know the procedure about how to read the parameter value from properties file in java.

Using jdk1.7 in eclipse.

user207421
  • 305,947
  • 44
  • 307
  • 483
karthi
  • 1
  • [http://www.java2s.com/Tutorial/Java/0220__I18N/AnInternationalizedSwingApplication.htm] look at this URL – Rahul Bhawar Feb 05 '16 at 06:12
  • 4
    Please check [here](http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/). Possible duplicate of [this](http://stackoverflow.com/questions/1318347/how-to-use-java-property-files) – Rao Feb 05 '16 at 06:18
  • Notepad has diddly-squat to do with this. – user207421 Feb 05 '16 at 09:47

1 Answers1

1

See the below code for reading config.properties file. Property file can be any where in the file system. You have to give the correct absolute path to the properties file.

import java.io.*;
import java.util.Properties;

class ConfigReader
{
    public static void main(String args[])
    {
        try
        {
            InputStream inputstream;
            Properties prop = new Properties();
            inputstream = new FileInputStream("pathtofile\\config.properties");
            if(inputstream!=null)
            {
                prop.load(inputstream);
                String property1 = prop.getProperty("property1");
                String property2 = prop.getProperty("property2");
                System.out.println(property1+"\n"+property2);
            }
            else
            {
                System.out.println("File not found");
            }

        }
        catch(Exception e)
        {
            System.out.println("Error");
        }

    }


}
Tapan Prakash
  • 773
  • 8
  • 7
  • properties are most often placed somewhere on the classpath, so you can use MyClass.class.getClassLoader().getResourceAsStream(filename); – sashok_bg Feb 05 '16 at 08:44