如何将包含一堆对象描述的文件加载到数组中?
How do I load a file with a bunch of descriptions for objects into an array?
我目前正在尝试编写我自己的魔法物品生成器供个人使用。现在我一直在尝试将一堆法术和对象描述加载到数组中,如果 spell/object 被卷入魔法项目创建,这些数组将从数组中调用。
我目前无法尝试将多个描述加载到一个数组中。我似乎只能在它退出之前将一个描述放入数组中。
目前我有这个代码:
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?
@@@
在循环的顶部,
while (!br.readLine().equals("@@@")) {
您调用 readLine()
,使用 1-liner 行,但您没有捕获行本身来处理它。
这里是修改后的代码:
示例文件:
@这是对法术的描述
我经常想知道我能多久写一次often.Without这个词,似乎我写这个太频繁了。
是不是觉得很奇怪。敏捷的棕色狐狸跳过红色的小栅栏。
@这是对法术的另一种描述
苹果是水果。
它是红色的。
@第三个描述
我有一只宠物。
是一只叫约翰的狗。
@@@
public static ArrayList<String> descriptions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File filename = new File("C:\temp\test.txt");
loadDescriptions(filename);
System.out.println("*************");
for(String ln:descriptions){
System.out.println(ln);
}
}
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;
String line=null;
while ((line = br.readLine()) != null ) {
if(line.startsWith("@")){
if(i>0){
descriptions.add(sb.toString());
sb = new StringBuilder();
}
}else{
if(!line.isEmpty()){
System.out.println(line);
sb.append(" " + line);
}
}
i++;
}
}
我目前正在尝试编写我自己的魔法物品生成器供个人使用。现在我一直在尝试将一堆法术和对象描述加载到数组中,如果 spell/object 被卷入魔法项目创建,这些数组将从数组中调用。
我目前无法尝试将多个描述加载到一个数组中。我似乎只能在它退出之前将一个描述放入数组中。
目前我有这个代码:
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?
@@@
在循环的顶部,
while (!br.readLine().equals("@@@")) {
您调用 readLine()
,使用 1-liner 行,但您没有捕获行本身来处理它。
这里是修改后的代码:
示例文件:
@这是对法术的描述
我经常想知道我能多久写一次often.Without这个词,似乎我写这个太频繁了。 是不是觉得很奇怪。敏捷的棕色狐狸跳过红色的小栅栏。
@这是对法术的另一种描述
苹果是水果。 它是红色的。
@第三个描述
我有一只宠物。 是一只叫约翰的狗。
@@@
public static ArrayList<String> descriptions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File filename = new File("C:\temp\test.txt");
loadDescriptions(filename);
System.out.println("*************");
for(String ln:descriptions){
System.out.println(ln);
}
}
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;
String line=null;
while ((line = br.readLine()) != null ) {
if(line.startsWith("@")){
if(i>0){
descriptions.add(sb.toString());
sb = new StringBuilder();
}
}else{
if(!line.isEmpty()){
System.out.println(line);
sb.append(" " + line);
}
}
i++;
}
}