【发布时间】:2016-04-25 14:24:04
【问题描述】:
我目前正在尝试编写我自己的魔法物品生成器供个人使用。现在我被困在尝试将一堆法术和对象描述加载到数组中,如果法术/对象被滚动到魔法物品的创建中,这些数组将从数组中调用。
我目前正试图将多个描述加载到一个数组中。在它退出之前,我似乎只能将一个描述放入数组中。
目前我有这个代码:
public class LoadDescription {
public static ArrayList<String> descriptions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File filename = new File("spellconcept.txt");
loadDescriptions(filename);
System.out.println(descriptions.get(0));
// System.out.println(descriptions.get(1));
}
public static void loadDescriptions(File name) throws IOException {
FileReader fr = new FileReader(name);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
int i = 0;
while (!br.readLine().equals("@@@")) {
try {
String line = br.readLine();
while (!line.isEmpty()) {
sb.append(" " + line);
line = br.readLine();
}
} catch (IOException e) {
}
;
descriptions.add(sb.toString());
}
i++;
}}
这是我尝试使用的文本文件。请忽略它缺乏智能,它只是一个测试文件:
@This is a description of a spell.
I often wonder how often I can write
the word often.Without seeming that it is too often that I write this out.
Does it not seem weird. The quick brown fox jumps over the small red fence.
@This is another description of a spell
@Maybe add another line here, \n see if this works? maybe?
@@@
【问题讨论】:
标签: java arrays file bufferedreader