0

All - I am a newbie to java. So need some help or code where the properties file is like test.properties 100 200 300 400

I want to read it into an single array, so that the input data that I get, i can check if its within the array or not.

I could actually hard code the like if id=100 or id=200 or id=300 {then do somethings} else { do something ordo nothing} .

I was able to find the answer for it: Going to add the code here

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class read_properties_into_array {

    private static List<String> sensitivePropertiesList=new ArrayList<String>();
    public static void main(String[] args) {
        try {
            File file = new File("test.properties");
            FileInputStream fileInput = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInput);
            fileInput.close();

            Enumeration enuKeys = properties.keys();
            while (enuKeys.hasMoreElements()) {
                String key = (String) enuKeys.nextElement();
                sensitivePropertiesList.add(new String(key));
                //String value = properties.getProperty(key);
                //System.out.println(key);
            }
            System.out.println("hi I am here");
            System.out.println("lenght of list:"+sensitivePropertiesList.size());

            for(int i=0;i<sensitivePropertiesList.size();i++)
            {
                System.out.println(sensitivePropertiesList.get(i));
            }
            System.out.println("Check if 100 it exists.");
            if (sensitivePropertiesList.contains("100"))
            {

                System.out.println(" 100 it exists.");
            }
            else 
            {
                System.out.println(" 100 Does not exist.");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Please add the test.properties file at the java project level if using eclipse.

Test.properties
100
200
300
user3539924
  • 61
  • 1
  • 7

2 Answers2

1

Though your question is not clear, I think you don't need a properties class to read an array from. You put key=value pairs in a properties file.

You should first read a file using java IO, then put all values in an array and finally iterate over that array and check for your value.

Check for some code here: https://stackoverflow.com/a/7705672/841221

Community
  • 1
  • 1
Aakash
  • 2,029
  • 14
  • 22
0

If you don't use a Java Properties file, but rather something like

test.properties

100
200
300

You could read all lines into a List and work later on that list.

Path inputFile = Paths.get("test.properties");
Charset fileCharset = Charset.defaultCharset();
List<String> allValues = Files.readAllLines(inputFile, fileCharset);

// work on that list
for (String value : allValues) {
    System.out.println(value);
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69