Log in
updated 11:29 AM UTC, May 4, 2016

How to read a Property file in Java?

  • Written by Madhu V Rao
  • Published in Core Java

How to read a Property file in Java and convert it to Set for comparison?

This program explains reading a property file and converting it to Set , so that it can be used for processing later.

class PropertyFileTest
{
    static Set<String> tagsMap = null;

    public static void main(String[] args)
    {
        Properties properties = new Properties();
        try
        {
            properties.load(new FileInputStream("listOfTags.properties"));
            String str = properties.getProperty("tags");
            String[] tags = str.split(",");
            List<String> list = Arrays.asList(tags);
            tagsMap = new HashSet<String>(list);
        }
        catch (IOException e)
        {
            System.out.println("NO FILE listOfTags.properties");
        }

        for(int i=0;i<args.length;i++)
        {
                if(tagsMap.contains(args[0]))
                {
                     System.out.println("Args specified are present in the property file: " + args[i]);
                }
         }
    }
}



Property file listOfTags.properties:

tags=tag1,tag2,tag3,tag4,tag5,etc



Compile and run:
javac PropertyFileTest.java
java PropertyFileTest tag1, tag3

Result:
Args specified are present in the property file: tag1
Args specified are present in the property file: tag3


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest