I am trying to get 2D array data from a Java properties file and store these data into a HashMap object.The thing that makes me puzzled is that Eclipse only retrieves only some of the data from the 2D array.
Here are the codes:
Properties config = new Properties();
config.load(new FileInputStream("categories.properties"));
String[][] categories = fetchArrayFromPropFile("content", config);
HashMap<String,Integer> hashmap = new HashMap<String,Integer>();
for (String[] mapping : categories)
{
hashmap.put(mapping[0], 0);
}
private static String[][] fetchArrayFromPropFile(String propertyName, Properties propFile) {
String[] a = propFile.getProperty(propertyName).split(";");
String[][] array = new String[a.length][a.length];
for(int i = 0;i < a.length;i++) {
array[i] = a[i].split(",");
}
return array;
}
In categories.properties file:
content=Advertising,0;Agriculture,0;Art,0;Automotive,0;Aviation,0;Banking,0;Beverages,0;Biotechnology,0;Business,0;Crime,0;Disasters,0;Economics,0;Education,0;Elections,0;Fashion,0;Food,0;Hardware,0;Health,0;Hotels,0;Intellectual Property,0;Investing,0;Labor,0;
Law,0;Marriage,0;Mobile Devices,0;Politics,0;Real Estate,0;Renewable Energy,0;Robotics,0;Science,0;Social Media,0;Software and Internet,0;Space,0;Sports,0;Technology,0;Traditional Energy,0;Travel,0;Video Games,0;War,0;Weather,0;Entertainment,0;No Topic,0; Business_Finance,0;Disaster_Accident,0;Entertainment_Culture,0;Environment,0;Health_Medical_Pharma,0;Hospitality_Recreation,0;Human Interest,0;Law_Crime,0;Religion_Belief,0;Technology_Internet,0;War_Conflict,0;Other,0;
It is because your property file format is
You see, it is split into two lines for one property. So when reading your content property, it reads only the first line and all data are not contained.
If so, please concatenated your property file into one line manually like this.
If so, your code works fine and all data are read.