Gew*_*ure 0 java arraylist properties-file
我正在使用此方法从属性文件中读取:
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要写入的字段声明如下:
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")
);
Run Code Online (Sandbox Code Playgroud)
..所以编译器向我展示了以下信息,我不知道如何使用:
不兼容的类型。必需,
ArrayList<String>但 'asList' 被推断为List<T>:不存在类型变量 T 的实例,因此List<T>符合ArrayList<String>
如何规避这种情况?
问题在于您分配给的声明变量的类型
emails = Arrays.asList(emailS.split(","));。
根据编译错误,它被声明为ArrayList但Arrays.asList()返回一个List.
您不能将 a 分配List给 anArrayList而您可以相反。
除了Arrays.asList()返回一个私有类的实例:java.util.Arrays.ArrayList这与ArrayList您声明的变量不同:java.util.ArrayList。因此,即使是沮丧的 to ArrayList也不会起作用并导致ClassCastException.
从更改声明的变量
ArrayList<String> emails ,以List<String> emails做同样的事情另外两个ArrayList变量。
| 归档时间: |
|
| 查看次数: |
5506 次 |
| 最近记录: |