我无法让这段代码工作。它正在读取的 .txt 文件在这里: urls.txt
这段代码的问题是它根本没有添加任何行。每当我尝试从“列表”中获取任何内容时,它都会给我一个 IndexOutOfBoundsException。
代码每 30 秒执行一次,所以我必须调用 lists.clear(); 方法。
注意:“列表”在前面定义:
public static ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
这是我的代码:
try {
URL urls = new URL("https://dl.dropboxusercontent.com/u/22001728/server%20creator/urls.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(urls.openStream()));
lists.clear();
String line;
ArrayList<String> list = new ArrayList<String>();
lists.add(list);
int y = 0, z = 0;
while ((line = br.readLine()) != null) {
y++;
if(line.equalsIgnoreCase(">")) {
System.out.print("Received command: INSERT ");
lists.add(list);
list.clear();
z++;
System.out.print(" ; Jumping to " + z + "\n");
} else {
System.out.println("Line: " + line + " added to lists[" + z + "].");
lists.get(z).add(line);
}
}
int selectedIndex = comboModel0.getIndexOf(comboModel0.getSelectedItem());
comboModel0.removeAllElements();
System.out.println("\n\n\n");
System.out.println(lists.get(1).get(1));
System.out.println("\n\n\n");
} catch (IOException e) {
System.out.println("Failed to download URLS data.");
}