gpt4 book ai didi

java - 从 java 属性文件加载 ArrayList

转载 作者:行者123 更新时间:2023-12-02 15:08:10 25 4
gpt4 key购买 nike

我正在使用这种方法读取属性文件:

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

try {
input = new FileInputStream(path);

prop.load(input);

/*saved like this:*/ //emails: abc@test.com, bbc@aab.com, ..
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 !!

} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

要写入的字段声明如下:

    private ArrayList<String> emails = new ArrayList<>(
Arrays.asList("f00@b4r.com", "test@test.com")
);

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

private LinkedList<String> feeds = new LinkedList<>(
Arrays.asList("http://www.kb.cert.org/vulfeed",
"https://ics-cert.us-cert.gov/advisories/advisories.xml")
);

..所以编译器向我显示了以下信息,我不知道如何使用:

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

如何规避这个?

最佳答案

问题是你分配给声明变量的类型 emails = Arrays.asList(emailS.split(",")); .
根据编译错误声明为ArrayList但是Arrays.asList()返回 List .
您不能分配 ListArrayList而你可以反过来。

此外Arrays.asList()返回私有(private)类的实例:java.util.Arrays.ArrayList这与 ArrayList 不同您声明的变量:java.util.ArrayList .于是偶垂头丧气地ArrayList不会工作并导致 ClassCastException .

改变声明的变量从 ArrayList<String> emailsList<String> emails对另外两个做同样的事情ArrayList变量。

关于java - 从 java 属性文件加载 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com