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