Gewure Gewure - 28 days ago 9
Java Question

loading ArrayList<String> from java properties file

I'm reading from a properties-file using this method:

public void loadConfigFromFile(String path) {
Properties prop = new Properties();
InputStream input = null;

try {

//input = new FileInputStream("");
input = new FileInputStream(path);

// load a properties file

// .... saved like this:
// emails:,, .....
String wordsS = prop.getProperty("keywords");
String emailS = prop.getProperty("emails");
String feedS = prop.getProperty("feeds");

emails = Arrays.asList(emailS.split(",")); //ERROR !!
words = Arrays.asList( wordsS.split(","))); //ERROR !!
feeds = Arrays.asList( feedS.split(",")); //ERROR !!
allXHours = Float.valueOf(prop.getProperty("frequency"));

} catch (IOException ex) {
} finally {
if (input != null) {
try {
} catch (IOException e) {

the fields to be written are declared as follows:

private ArrayList<String> emails = new ArrayList<>(
Arrays.asList("", "")

private LinkedList<String> words = new LinkedList<>(
Arrays.asList("vuln", "banana", "pizza", "bonanza")

private LinkedList<String> feeds = new LinkedList<>(
); the compiler shows me the following Information, which I don't know how to use:

Incompatible types. Required
but 'asList' was inferred to
: no instance(s) of type variable(s) T exist so that
conforms to

How to circumvent this?

Answer Source

The problem is the type of the declared variable that you assign to emails = Arrays.asList(emailS.split(","));.
According to the compilation error, it is declared as ArrayList but Arrays.asList() returns a List.
You cannot assign a List to an ArrayList while you can do the reverse.

Besides Arrays.asList() return an instance of a private class : java.util.Arrays.ArrayList that is not the same that the ArrayList variable that you declare : java.util.ArrayList. So even a downcast to ArrayList would not work and cause a ClassCastException.

Change the declared variable from ArrayList<String> emails to List<String> emails and do the same thing for two other ArrayList variables.